How to delete list item using SharePoint Online 2013 Rest API in C# -
i trying delete list item sharepoint 2013 online using rest apis c# managed code.
here's essence of code:
using (var client = new webclient()) { client.headers.add("x-forms_based_auth_accepted", "f"); client.credentials = myspcreds; client.headers.add(httprequestheader.contenttype, "application/json;odata=verbose"); client.headers.add(httprequestheader.accept, "application/json;odata=verbose"); client.headers.add("x-http-method", "delete"); client.headers.add("if-match", "*"); var requesturi = new uri("https://mysharepointsite.../_api/web/lists/getbytitle('mylist')/items(123)"); client.uploadstring(requesturi, string.empty); }
i'm getting 403 permission denied, using similar pattern able create list item. using credentials webclient sharepointonlinecredentials object. sharepoint site administrator.
so, wondering if have syntax/approach wrong. can verify code above "should" work assuming don't have permissions issues?
ok, found solution:
the client headers need include form digest:
client.headers.add("x-requestdigest", getformdigest());
to form digest, can use code:
private string getformdigest() { var webclient = new webclient(); webclient.headers.add("x-forms_based_auth_accepted", "f"); webclient.credentials = this.credentials; webclient.headers.add(httprequestheader.contenttype, string.format("application/json;odata=nometadata")); webclient.headers.add(httprequestheader.accept, string.format("application/json;odata=nometadata")); var uri = new uri(this.sharepointbaseurl); var endpointuri = new uri(uri, "_api/contextinfo"); var result = webclient.uploadstring(endpointuri, "post"); jtoken t = jtoken.parse(result); //use if odata = nometadata return t["formdigestvalue"].tostring(); //use if odata = verbose //return t["d"]["getcontextwebinformation"]["formdigestvalue"].tostring(); }
note: credentials tricky too, here's code that:
var securepassword = new securestring(); foreach (var c in mypassword) { securepassword.appendchar(c); } this.credentials = new sharepointonlinecredentials(myusername, securepassword);
Comments
Post a Comment