Hello,
is it possible to update/modify a list item in a document library via REST Api (c#)?
Unfortunately, I could not find any code samples.
Any kind of help will be appreciated.
Thanks,
Dennis
Technology Tips and News
Hello,
is it possible to update/modify a list item in a document library via REST Api (c#)?
Unfortunately, I could not find any code samples.
Any kind of help will be appreciated.
Thanks,
Dennis
Hi Dennis,
Yes, it is possible to update list item using REST. Please find a simple code sample below to update the title of an item using REST call -
- // Update Operation
- // listName: The name of the list you want to get items from
- // siteurl: The url of the site that the list is in. // title: The value of the title field for the new item
- // itemId: the id of the item to update
- // success: The function to execute if the call is sucesfull
- // failure: The function to execute if the call fails
- function updateListItem(itemId, listName, siteUrl, title, success, failure) {
- var itemType = GetItemTypeForListName(listName);
- var item = {
- "__metadata": { "type": itemType },
- "Title": title
- };
- getListItemWithId(itemId, listName, siteUrl, function (data) {
- $.ajax({
- url: data.__metadata.uri,
- type: "POST",
- contentType: "application/json;odata=verbose",
- data: JSON.stringify(item),
- headers: {
- "Accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "MERGE",
- "If-Match": data.__metadata.etag
- },
- success: function (data) {
- success(data);
- },
- error: function (data) {
- failure(data);
- }
- });
- }, function(data){
- failure(data);
- });
- }
- function GetItemTypeForListName(name) {
- return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
- }
Narahari
*******If a post answers your question, please click "Mark As Answer" on that post and/or "Vote as Helpful *********
Thank you both very much for your replies.
@Narahari: That's exactly what I am looking for, but for C# not JavaScript. Unfortunately, I was not able to transfer the code to C#. Do you have a C# sample as well?
Thanks,
Dennis
C# examples are here:
http://msdn.microsoft.com/en-us/library/jj164022.aspx
It's possible, just leverage HttpWebRequest and HttpResponse object, make a HTTP POST call
HttpWebRequest endpointRequest =
(HttpWebRequest)HttpWebRequest.Create(
"http:// <site url>/_api/contextinfo");
endpointRequest.Method = "POST";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Add("Authorization",
"Bearer " + accessToken);
HttpWebResponse endpointResponse =
(HttpWebResponse)endpointRequest.GetResponse();
Then set the necessary headers in the request
Hi Dennis,
Please find below a sample code in C# which updates the list title using REST calls. You can update the code (string data and url) in the sample below to update a list item also.
Uri uri = new Uri("http://sp2013vm/_api/web/lists/GetByTitle('Tasks')"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = "application/json;odata=verbose"; request.Headers["X-RequestDigest"] = getDigestValue(); request.Headers["X-HTTP-Method"] = "MERGE"; request.Headers["IF-MATCH"] = "*"; request.Credentials = CredentialCache.DefaultCredentials; request.Accept = "application/json;odata=verbose"; request.Method = "POST"; string stringData = "{ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }"; request.ContentLength = stringData.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(stringData); writer.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream());
Narahari
*******If a post answers your question, please click "Mark As Answer" on that post and/or "Vote as Helpful *********
Hi,
Thanks for the post,
Actually i need to send a msdocument to office365 document library by using rest api at client side.
how to attach file to Sharepoint online [office 365] document library by using visual studio 2010 & Rest API services in visual studio?
Can anybody please tell me its urgent i have been searching for this task since one week,please help me
atleast u tell me how to connect to office365 online through java script or any csom with rest api only.
please help me.
Thanking you in Advance
Mahesh