2015-07-02 12 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace WindowsFormsApplication5 
{ 
    public class ClientContext 
    { 
     private string p; 

     public ClientContext(string p) 
     { 
      // TODO: Complete member initialization 
      this.p = p; 
     } 
    } 

    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 


    //First construct client context, the object which will be responsible for 
    //communication with SharePoint: 
     private ClientContext context = new ClientContext("@url"); 


    //then get a hold of the list item you want to download, for example 
    public List list; 
     public ClientContext 
     { 
      list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING"); 
     } 

    //note that data has not been loaded yet. In order to load the data 
    //you need to tell SharePoint client what you want to download: 

    context.Load(result, items=>items.Include(
     item => item["Title"], 
     item => item["FileRef"] 
    )); 

    //now you get the data 
    context.ExecuteQuery(); 

    //here you have list items, but not their content (files). To download file 
    //you'll have to do something like this: 

    var item = items.First(); 

    //get the URL of the file you want: 
    var fileRef = item["FileRef"]; 

    //get the file contents: 
    FileInformation fileInfo = File.OpenBinaryDirect(context, fileRef.ToString()); 

    using (var memory = new MemoryStream()) 
    { 
      byte[] buffer = new byte[1024 * 64]; 
      int nread = 0; 
      while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       memory.Write(buffer, 0, nread); 
      } 
      memory.Seek(0, SeekOrigin.Begin); 
      // ... here you have the contents of your file in memory, 
      // do whatever you want 
    } 
    } 
} 

這是完整的代碼。 我不知道它爲什麼顯示錯誤。我搜索了錯誤「是一個字段,但用作類型」,我嘗試了,但它沒有幫助。請幫忙解決這個問題,因爲我是新手。先謝謝你。錯誤:是一個字段,但用作類型

+0

我很抱歉,我不得不說這一點;但是您粘貼的代碼出錯了,或者存在多個嚴重錯誤。我必須認爲這是最後的選擇。在這種情況下,最好從一個基本的C#教程開始,爲自己找出爲什麼這是錯誤的。 –

+0

有許多語法錯誤,你的代碼根本無法生成。 –

+0

你想在這裏實現什麼?調用包含這些列的列表?然後!我想你應該更新你的問題,因爲標題不符合描述。 –

回答

0

你想通過這段代碼來實現什麼?

public partial class Form1 : Form 
{ 
    ... 
    public ClientContext 
    { 
     list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING"); 
    }  
} 

什麼是public ClientContext {}類Form1內?

看來您打算在另一個類中爲類創建構造函數,並且對於編譯器來說,它看起來更像是一個屬性,但沒有訪問器(get,set),就好像它是Type或者像這樣。

試着把get;組;

或更改爲方法,而不是:裏面如果您打算創建屬性訪問

public void GetClientContext() 
    { 
     list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING"); 
    } 
相關問題