//gavdcodeend 18 //gavdcodebegin 19 static void SpCsRestRenameOneFolder(Uri webUri, string userName, string password) { string myServerRelativeUrl = "/sites/[SiteName]/[LibraryName]/RestFolder"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = new { __metadata = new { type = "SP.Data.TestDocumentsItem" }, Title = "RestFolderRenamed", FileLeafRef = "RestFolderRenamend" }; string endpointUrl = webUri + "/_api/web/GetFolderByServerRelativeUrl('" + myServerRelativeUrl + "')/ListItemAllFields"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "MERGE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 02 //gavdcodebegin 03 static void SpCsRestDownloadOneDocument(Uri webUri, string userName, string password) { string webUrlRel = new Uri(webUri.ToString()).AbsolutePath; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/web/getfilebyserverrelativeurl(" + "'" + webUrlRel + "/TestLibrary/TestDocument01.docx')" + "/$value"; Stream data = (Stream)client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload, true); byte[] result; using (var streamReader = new MemoryStream()) { data.CopyTo(streamReader); result = streamReader.ToArray(); } FileStream outputStream = new FileStream(@"C:\Temporary\TestDwload.docx", FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None); outputStream.Write(result, 0, result.Length); outputStream.Flush(true); outputStream.Close(); } }
//gavdcodeend 22 //gavdcodebegin 23 static void SpCsRestDownloadOneAttachmentByFileName(Uri webUri, string userName, string password) { string webUrlRel = new Uri(webUri.ToString()).AbsolutePath; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { string myFileName = "Test.csv"; string myFilesPath = @"C:\Temporary\"; object myPayload = null; string endpointUrl = webUri + "/_api/lists/GetByTitle('TestList')" + "/items(3)/AttachmentFiles('" + myFileName + "')" + "/$value"; Stream data = (Stream)client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload, true); byte[] result; using (var streamReader = new MemoryStream()) { data.CopyTo(streamReader); result = streamReader.ToArray(); } FileStream outputStream = new FileStream(myFilesPath + myFileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None); outputStream.Write(result, 0, result.Length); outputStream.Flush(true); outputStream.Close(); } }
//gavdcodeend 15 //gavdcodebegin 16 static void SpCsRestDeleteUserFromSecurityRoleInListItem(Uri webUri, string userName, string password) { // Find the User int userId = 0; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/web/siteusers?$select=Id&" + "$filter=startswith(Title,'MOD')"; var data = (JObject)client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload); userId = int.Parse(data["d"]["results"][0]["Id"].ToString()); Console.WriteLine(userId); } // Remove the User from the List using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/web/lists/getbytitle" + "('TestList')/items(17)/roleassignments/getbyprincipalid(" + "principalid=" + userId + ")"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "DELETE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 06 //gavdcodebegin 07 static void SpCsRestReadOneLibraryDoc(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/getbytitle('TestLibrary')" + "/items(22)?$select=Title,Id"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload); Console.WriteLine(data); } }
//gavdcodeend 21 //gavdcodebegin 22 static void SpCsRestReadAllAttachments(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/GetByTitle('TestList')" + "/items(3)/AttachmentFiles"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload); Console.WriteLine(data); } }
//gavdcodeend 12 //gavdcodebegin 13 static void SpCsRestResetSecurityInheritanceListItem(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/getbytitle('TestList')/" + "items(17)/resetroleinheritance"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, myPayload); Console.WriteLine(data); } }
//gavdcodeend 20 //gavdcodebegin 21 static void SpCsRestCreateOneAttachment(Uri webUri, string userName, string password) { string myFilePath = @"C:\Temporary\Test.csv"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/GetByTitle('TestList')" + "/items(3)/AttachmentFiles/add(FileName='" + myFilePath + "')"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, myPayload); Console.WriteLine(data); } }
//gavdcodeend 17 //gavdcodebegin 18 static void SpCsRestReadAllFolders(Uri webUri, string userName, string password) { string myServerRelativeUrl = "/sites/[SiteName]/[LibraryName]/RestFolder"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/web/GetFolderByServerRelativeUrl('" + myServerRelativeUrl + "')/ListItemAllFields"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Get, myPayload); Console.WriteLine(data); } }
//gavdcodebegin 01 static void SpCsRestCreateOneListItem(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = new { __metadata = new { type = "SP.ListItem" }, Title = "NewListItemCsRest" }; string endpointUrl = webUri + "/_api/web/lists/getbytitle('TestList')" + "/items"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, myPayload); Console.WriteLine(data); } }
//gavdcodeend 01 //gavdcodebegin 02 static void SpCsRestUploadOneDocument(Uri webUri, string userName, string password) { FileInfo myFileInfo = new FileInfo(@"C:\Temporary\TestDocument01.docx"); string webUrlRel = new Uri(webUri.ToString()).AbsolutePath; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { Stream myPayload = System.IO.File.OpenRead(myFileInfo.FullName); string endpointUrl = webUri + "/_api/web/getfolderbyserverrelativeurl(" + "'" + webUrlRel + "/TestLibrary')/files/add(url='" + myFileInfo.Name + "',overwrite=true)"; var data = client.ExecuteJson <Stream>(endpointUrl, HttpMethod.Post, myPayload); Console.WriteLine(data); } }
//gavdcodeend 16 //gavdcodebegin 17 static void SpCsRestCreateOneFolder(Uri webUri, string userName, string password) { string myServerRelativeUrl = "/sites/[SiteName]/[LibraryName]/RestFolder"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = new { __metadata = new { type = "SP.Folder" }, ServerRelativeUrl = myServerRelativeUrl }; string endpointUrl = webUri + "/_api/web/Folders"; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, myPayload); Console.WriteLine(data); } }
//gavdcodeend 10 //gavdcodebegin 11 static void SpCsRestDeleteOneLibraryDoc(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/getbytitle('TestLibrary')" + "/items(22)"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "DELETE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 11 //gavdcodebegin 12 static void SpCsRestBreakSecurityInheritanceListItem(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/getbytitle('TestList')/" + "items(17)/breakroleinheritance(copyRoleAssignments=false," + "clearSubscopes=true)"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "MERGE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 23 //gavdcodebegin 24 static void SpCsRestDeleteOneAttachmentByFileName(Uri webUri, string userName, string password) { string myFileName = "Test.csv"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/lists/GetByTitle('TestList')" + "/items(3)/AttachmentFiles('" + myFileName + "')"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "DELETE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 19 //gavdcodebegin 20 static void SpCsRestDeleteOneFolder(Uri webUri, string userName, string password) { string myServerRelativeUrl = "/sites/[SiteName]/[LibraryName]/RestFolder"; using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = null; string endpointUrl = webUri + "/_api/web/GetFolderByServerRelativeUrl('" + myServerRelativeUrl + "')"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "DELETE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }
//gavdcodeend 08 //gavdcodebegin 09 static void SpCsRestUpdateOneLibraryDoc(Uri webUri, string userName, string password) { using (SPHttpClient client = new SPHttpClient(webUri, userName, password)) { object myPayload = new { __metadata = new { type = "SP.ListItem" }, Title = "TestDocument01_Updated.docx" }; string endpointUrl = webUri + "/_api/lists/getbytitle('TestLibrary')/" + "items(22)"; IDictionary <string, string> headers = new Dictionary <string, string> { { "IF-MATCH", "*" }, { "X-HTTP-Method", "MERGE" } }; var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, myPayload); Console.WriteLine(data); } }