[英]Sharepoint/Rest and metadata
I am doing some basic Rest/http calls to a sharepoint site to upload documents. 我正在对共享点站点进行一些基本的Rest / http调用以上传文档。 See code below.
请参见下面的代码。
This works like a charm, but it only creates the file with the name given and the content. 这就像一个超级按钮,但是只能使用给定的名称和内容创建文件。 Is there a way to manipulate the meta data in sharepoint via the REST/http interface?
有没有一种方法可以通过REST / http接口在共享点中操作元数据?
I also tried using the oData protocol which makes it easy to alter metadata, but that required me to add a service reference and use the generated classes, very nice, but not the flexibility I need. 我还尝试使用oData协议,该协议使更改元数据变得容易,但这需要我添加服务引用并使用生成的类,这非常好,但我却没有灵活性。 Any suggestions?
有什么建议么?
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.
Once you have uploaded the file if you search the response data you can find a server reply that contains the ID assigned to the file in SharePoint. 上载文件后,如果您搜索响应数据,则可以找到服务器答复,其中包含在SharePoint中分配给该文件的ID。 With this ID we can make a second rest call to update that item and its metadata.
使用此ID,我们可以再次调用rest来更新该项目及其元数据。
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)
{
}
}
I have more examples here of working with SharePoint REST using the HttpClient: 我这里有更多使用HttpClient处理SharePoint REST的示例:
https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/ https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.