2012-06-17 59 views
0

我做一些基本的REST/HTTP調用到SharePoint網站上傳文檔。見下面的代碼。的Sharepoint/REST和元數據

這個工程就像一個魅力,但它只能創建一個具有給定的名稱和內容的文件。有沒有辦法通過REST/http界面操作Sharepoint中的元數據?

我也嘗試使用OData協議,這使得很容易改變的元數據,但這需要我添加一個服務引用,並使用生成的類,很漂亮,但不是我所需要的靈活性。有什麼建議麼?

byte[] bytesToSend = // get bytes from a file somewhere 
WebRequest req = WebRequest.Create(new Uri("https://mysharepoint/sites/mysite/file.txt")); 
req.Method = "PUT"; 
req.Credentials = new NetworkCredential("USR","passwd","Domain"); 
MemoryStream ms = new MemoryStream(bytesToSend); 
ms.CopyTo(req.GetRequestStream()); 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
if (resp.StatusCode.Equals(HttpStatusCode.OK) || resp.StatusCode.Equals(HttpStatusCode.Created)) 
{  // ... Ok done uploading. 
+1

你有沒有嘗試添加標題? – Paparazzi

+0

好吧,不。這顯然是要走的路。只需要找到一些標題名稱和他們的語法。 –

+0

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype。 ASPX – Paparazzi

回答

0

如果搜索到響應數據,則上傳文件後,可以找到包含分配給SharePoint中文件的標識的服務器回覆。有了這個ID,我們可以進行第二次休息,以更新該項目及其元數據。

if (response.IsSuccessStatusCode) 
{ 
    var info = response.Content.ReadAsStringAsync(); 
    JsonObject d = JsonValue.Parse(info.Result).GetObject(); 
    string id = d["d"].GetObject()["ListItemAllFields"].GetObject().GetNamedValue("ID").Stringify(); 

    client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); 
    client.BaseAddress = new System.Uri(url); 
    client.DefaultRequestHeaders.Clear(); 
    client.DefaultRequestHeaders.Add("X-RequestDigest", digest); 
    client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); 
    client.DefaultRequestHeaders.Add("IF-MATCH", "*"); 
    HttpContent strContent = new StringContent(String.Concat("{ '__metadata': { 'type': 'SP.List' }, 'Title': '", filename, "' }")); 
    strContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
    strContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("odata", "verbose")); 
    HttpResponseMessage updateResponse = await client.PostAsync(String.Concat("_api/web/lists/GetByTitle('Project Photos')/Items(", id, ")"), strContent); 
    updateResponse.EnsureSuccessStatusCode(); 
    if (updateResponse.IsSuccessStatusCode) 
    { 

    } 
} 

我有更多的例子在這裏使用的HttpClient使用SharePoint REST工作:

https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/