├── SpRestUtility ├── Models │ ├── SpFolder.cs │ ├── SpItem.cs │ ├── SpFile.cs │ ├── SpUser.cs │ ├── SpField.cs │ └── SpList.cs ├── Properties │ └── AssemblyInfo.cs ├── SpRestUtilities.csproj └── SpRestUtilities.cs ├── SpRestUtility.sln ├── LICENSE ├── .gitignore └── README.md /SpRestUtility/Models/SpFolder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SpRestUtility 4 | { 5 | public class SpFolder 6 | { 7 | public Dictionary Properties = new Dictionary(); 8 | 9 | public void SetProperty(string fieldName, string value) 10 | { 11 | if (Properties.ContainsKey(fieldName)) 12 | Properties[fieldName] = value; 13 | else 14 | Properties.Add(fieldName, value); 15 | } 16 | } 17 | public class SpFolderCollection 18 | { 19 | public List Folders = new List(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /SpRestUtility/Models/SpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SpRestUtility 4 | { 5 | public class SpItem 6 | { 7 | public int Id { get; set; } 8 | public Dictionary Data = new Dictionary(); 9 | 10 | public void SetFieldValue(string fieldName, string value) 11 | { 12 | if (Data.ContainsKey(fieldName)) 13 | Data[fieldName] = value; 14 | else 15 | Data.Add(fieldName, value); 16 | } 17 | } 18 | 19 | public class SpItemCollection 20 | { 21 | public List Items = new List(); 22 | } 23 | } -------------------------------------------------------------------------------- /SpRestUtility/Models/SpFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace SpRestUtility 8 | { 9 | public class SpFile 10 | { 11 | public Dictionary Properties = new Dictionary(); 12 | 13 | public void SetProperty(string fieldName, string value) 14 | { 15 | if (Properties.ContainsKey(fieldName)) 16 | Properties[fieldName] = value; 17 | else 18 | Properties.Add(fieldName, value); 19 | } 20 | } 21 | 22 | public class SpFileCollection 23 | { 24 | public List Files = new List(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SpRestUtility/Models/SpUser.cs: -------------------------------------------------------------------------------- 1 | namespace SpRestUtility 2 | { 3 | public class SpUser 4 | { 5 | public int Id { get; set; } 6 | public bool IsHiddenInUi { get; set; } 7 | public string LoginName { get; set; } 8 | public string Title { get; set; } 9 | public SpUserPrincipalType PrincipalType { get; set; } 10 | public string Email { get; set; } 11 | public bool IsSiteAdmin { get; set; } 12 | public string UserId { get; set; } 13 | } 14 | public enum SpUserPrincipalType 15 | { 16 | None = 0, 17 | User = 1, 18 | DistributionList = 2, 19 | SecurityGroup = 4, 20 | SharepointGroup = 8, 21 | All = 15 22 | } 23 | 24 | public class SpUserCollection 25 | { 26 | public List Users = new List(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /SpRestUtility.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpRestUtilities", "SpRestUtility\SpRestUtilities.csproj", "{6A04D40E-0C07-4EE5-9461-E451C2794F43}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {6A04D40E-0C07-4EE5-9461-E451C2794F43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {6A04D40E-0C07-4EE5-9461-E451C2794F43}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {6A04D40E-0C07-4EE5-9461-E451C2794F43}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {6A04D40E-0C07-4EE5-9461-E451C2794F43}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Urben 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /SpRestUtility/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SpRestUtility")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("ASBNET")] 12 | [assembly: AssemblyProduct("SpRestUtility")] 13 | [assembly: AssemblyCopyright("Copyright © ASBNET 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("6a04d40e-0c07-4ee5-9461-e451c2794f43")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SpRestUtility/Models/SpField.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SpRestUtility 4 | { 5 | public class SpField 6 | { 7 | public string Id { get; set; } 8 | public string InternalName { get; set; } 9 | public SpFieldTypeKind FieldTypeKind { get; set; } 10 | public Dictionary Properties = new Dictionary(); 11 | 12 | public void SetProperty(string propertyName, string value) 13 | { 14 | if (Properties.ContainsKey(propertyName)) 15 | Properties[propertyName] = value; 16 | else 17 | Properties.Add(propertyName, value); 18 | } 19 | } 20 | public enum SpFieldTypeKind 21 | { 22 | None = 0, 23 | Integer = 1, 24 | Text = 2, 25 | Note = 3, 26 | DateTime = 4, 27 | Counter = 5, 28 | Choice = 6, 29 | Lookup = 7, 30 | Boolean = 8, 31 | Number = 9, 32 | Currency = 10, 33 | URL = 11, 34 | Computed = 12, 35 | Threading = 13, 36 | Guid = 14, 37 | MultiChoice = 15, 38 | GridChoice = 16, 39 | Calculated = 17, 40 | File = 18, 41 | Attachments = 19, 42 | User = 20, 43 | Recurrence = 21, 44 | CrossProjectLink = 22, 45 | ModStat = 23, 46 | Error = 24, 47 | ContentTypeId = 25, 48 | PageSeparator = 26, 49 | ThreadIndex = 27, 50 | WorkflowStatus = 28, 51 | AllDayEvent = 29, 52 | WorkflowEventType = 30, 53 | MaxItems = 31 54 | } 55 | public class SpFieldCollection 56 | { 57 | public List Fields = new List(); 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /SpRestUtility/Models/SpList.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SpRestUtility 4 | { 5 | public class SpList 6 | { 7 | public string Id { get; set; } 8 | public SpListTemplateType SpListTemplateType { get; set; } 9 | public Dictionary Properties = new Dictionary(); 10 | 11 | public void SetProperty(string propertyName, string value) 12 | { 13 | if (Properties.ContainsKey(propertyName)) 14 | Properties[propertyName] = value; 15 | else 16 | Properties.Add(propertyName, value); 17 | } 18 | } 19 | public enum SpListTemplateType 20 | { 21 | InvalidType = -1, 22 | NoListTemplate = 0, 23 | GenericList = 100, 24 | DocumentLibrary = 101, 25 | Survey = 102, 26 | Links = 103, 27 | Announcements = 104, 28 | Contacts = 105, 29 | Events = 106, 30 | Tasks = 107, 31 | DiscussionBoard = 108, 32 | PictureLibrary = 109, 33 | DataSources = 110, 34 | WebTemplateCatalog = 111, 35 | UserInformation = 112, 36 | WebPartCatalog = 113, 37 | ListTemplateCatalog = 114, 38 | XMLForm = 115, 39 | MasterPageCatalog = 116, 40 | NoCodeWorkflows = 117, 41 | WorkflowProcess = 118, 42 | WebPageLibrary = 119, 43 | CustomGrid = 120, 44 | SolutionCatalog = 121, 45 | NoCodePublic = 122, 46 | ThemeCatalog = 123, 47 | DesignCatalog = 124, 48 | AppDataCatalog = 125, 49 | DataConnectionLibrary = 130, 50 | WorkflowHistory = 140, 51 | GanttTasks = 150, 52 | HelpLibrary = 151, 53 | AccessRequest = 160, 54 | TasksWithTimelineAndHierarchy = 171, 55 | MaintenanceLogs = 175, 56 | Meetings = 200, 57 | Agenda = 201, 58 | MeetingUser = 202, 59 | Decision = 204, 60 | MeetingObjective = 207, 61 | TextBox = 210, 62 | ThingsToBring = 211, 63 | HomePageLibrary = 212, 64 | Posts = 301, 65 | Comments = 302, 66 | Categories = 303, 67 | Facility = 402, 68 | Whereabouts = 403, 69 | CallTrack = 404, 70 | Circulation = 405, 71 | Timecard = 420, 72 | Holidays = 421, 73 | IMEDic = 499, 74 | ExternalList = 600, 75 | MySiteDocumentLibrary = 700, 76 | IssueTracking = 1100, 77 | AdminTasks = 1200, 78 | HealthRules = 1220, 79 | HealthReports = 1221, 80 | DeveloperSiteDraftApps = 1230 81 | } 82 | } -------------------------------------------------------------------------------- /SpRestUtility/SpRestUtilities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {6A04D40E-0C07-4EE5-9461-E451C2794F43} 8 | Library 9 | Properties 10 | SP_REST_UTILITY 11 | SP_REST_UTILITY 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sharepoint REST Utilities for C# 2 | The Sharepoint rest api is very powerful but writing code that does something with it can be tricky and costly sometimes. Everytime i wanted to use it i had to try to remember how to do this and that, looking through code i've written and of course googling. So i thought it would be great to have a class library which would do all the dirty work for me and this is what this project is basically all about. But I'm warning you in advance: This hasn't been fully tested! Use it on your own risk! It's also still work in progress and may or my be not further developed in the future. 3 | 4 | ## Getting Started 5 | After referencing this solution in your project, which can be basically anything, all you have to do is to initialize the SpRestUtilities and tell it where to operate (sp-site-url) and who you are (username and pw). 6 | 7 | ```c# 8 | using SpRestUtility; 9 | 10 | SpRestUtilities myUT = new SpRestUtilities(); 11 | myUT.SiteUrl = "https://yoursharepoint.com/yoursite"; 12 | myUT.Credentials = new NetworkCredential("USERNAME", "PASSWORD"); 13 | ``` 14 | 15 | ## List Utilities 16 | ### Get SpList by Title or its Id 17 | ```c# 18 | SpList listA = myUT.GetSpListByTitle("LISTNAME"); 19 | SpList listB = myUT.GetSpListByID("GUID"); // e.g.: "bacfa614-08de-428e-be54-24d673600901" 20 | ``` 21 | ### Changeable Listproperties 22 | The following Properties "should" be able to be set or changed: 23 | - Title 24 | - Description 25 | - ContentTypesEnabled 26 | - AllowContentTypes 27 | - EnableAttachments 28 | - EnableFolderCreation 29 | - EnableMinorVersions 30 | - EnableModeration 31 | - EnableVersioning 32 | - ForceCheckout 33 | - HasExternalDataSource 34 | - Hidden 35 | - IrmEnabled 36 | - IrmExpire 37 | - IrmReject 38 | - IsApplicationList 39 | - IsCatalog 40 | - IsPrivate 41 | - MultipleDataList 42 | - NoCrawl 43 | - ServerTemplateCanCreateFolders 44 | - BaseTemplate 45 | ### Create a new List 46 | ```c# 47 | SpList newList = new SpList(); 48 | newList.SpListTemplateType = SpListTemplateType.GenericList; 49 | newList.SetProperty("Title","LISTTITLE"); 50 | newList.SetProperty("Description","DESCRIPTION"); 51 | newList.SetProperty("AllowContentTypes", "true"); 52 | newList.SetProperty("ContentTypesEnabled", "false"); 53 | myUt.CreateSPList(newList); 54 | ``` 55 | ### Update a List 56 | ```c# 57 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 58 | list.SetProperty("Title","NEWTITLE"); 59 | myUT.UpdateSpList(list); 60 | ``` 61 | ### Delete a List 62 | ```c# 63 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 64 | myUT.DeleteSpList(list); 65 | ``` 66 | 67 | ## Library Utilities 68 | ### Get a SpFolder 69 | ```c# 70 | // With a Library 71 | SpList library = myUT.GetSpListByTitle("LIBRARYNAME"); 72 | SpFolder folder = myUT.GetSpFolderByPath("FOLDERNAME",library); 73 | 74 | // With path only 75 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 76 | 77 | // Get a Subfolder 78 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME/SUBFOLDERNAME"); 79 | ``` 80 | ### Get all SpFolders from SpFolder 81 | ```c# 82 | // With path and Library 83 | SpList library = myUT.GetSpListByTitle("LIBRARYNAME"); 84 | SpFolderCollection folderCollection = myUT.GetSpFolderCollectionByPath("FOLDERNAME",library); 85 | 86 | // With path only 87 | SpFolderCollection folderCollection = myUT.GetSpFolderCollectionByPath("LIBRARYNAME/FOLDERNAME"); 88 | 89 | // Get all Folders in Subfolder 90 | SpFolderCollection folderCollection = myUT.GetSpFolderCollectionByPath("LIBRARYNAME/FOLDERNAME/SUBFOLDERNAME"); 91 | 92 | // By SpFolder 93 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 94 | SpFolderCollection folderCollection = myUT.GetAllSpFoldersFromSpFolder(folder); 95 | ``` 96 | ### Create a new SpFolder 97 | ```c# 98 | // With path and Library 99 | SpList lilibrary = myUT.GetSpListByTitle("LIBRARYNAME"); 100 | SpFolder folder = myUT.CreateSpFolder("FOLDERNAME",library); 101 | 102 | // With path only 103 | SpFolder folder = myUT.CreateSpFolder("LIBRARYNAME/FOLDERNAME"); 104 | 105 | // Create a Subfolder 106 | SpFolder subFolder = myUT.CreateSpFolder("LIBRARYNAME/FOLDERNAME/SUBFOLDERNAME"); // Parent Folders must exist! 107 | ``` 108 | ### Delete a SpFolder 109 | ```c# 110 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 111 | myUT.DeleteSpFolder(folder); 112 | ``` 113 | ### Get a SpFile by FileName from SpFolder 114 | ```c# 115 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 116 | SpFile file = myUT.GetSpFileByFilenameFromSpFolder("FILENAME.txt",folder); 117 | ``` 118 | ### Get all SpFiles by FileName from SpFolder 119 | ```c# 120 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 121 | SpFileCollection file = myUT.GetAllSpFilesFromSpFolder(folder); 122 | ``` 123 | ### Upload a File into a SpFolder 124 | ```c# 125 | string path = @"C:\yourpath\FILENAME.txt"; 126 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 127 | 128 | // Auto overwrite 129 | SpFile file = myUT.UploadSpFileToSpFolder(path,folder); // if overwrite is undefined it is set to true 130 | 131 | // overwrite false 132 | SpFile file = myUT.UploadSpFileToSpFolder(path,folder,false); 133 | ``` 134 | ### Delete SpFile 135 | ```c# 136 | SpFolder folder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 137 | SpFile file = myUT.GetSpFileByFilenameFromSpFolder("FILENAME.txt",folder); 138 | myUT.DeleteSpFile(file); 139 | ``` 140 | ### Move SpFile to another SpFolder 141 | ```c# 142 | SpFolder sourceFolder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 143 | SpFile file = myUT.GetSpFileByFilenameFromSpFolder("FILENAME.txt",sourceFolder); 144 | SpFolder destFolder = myUT.GetSpFolderByPath("LIBRARYNAME/DESTINATIONFOLDERNAME"); 145 | myUT.MoveSpFile(file,destFolder); 146 | ``` 147 | ### Copy SpFile to another SpFolder 148 | ```c# 149 | SpFolder sourceFolder = myUT.GetSpFolderByPath("LIBRARYNAME/FOLDERNAME"); 150 | SpFile file = myUT.GetSpFileByFilenameFromSpFolder("FILENAME.txt",sourceFolder); 151 | SpFolder destFolder = myUT.GetSpFolderByPath("LIBRARYNAME/DESTINATIONFOLDERNAME"); 152 | 153 | // Auto overwrite 154 | myUT.CopySpFile(file,destFolder); 155 | 156 | // overwrite false 157 | myUT.CopySpFile(file,destFolder,false); 158 | ``` 159 | 160 | ## Item Utilities 161 | ### Get SpItem by ID 162 | ```c# 163 | int yourItemId = 100; 164 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 165 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 166 | ``` 167 | ### Get SpItemCollection by optional filter 168 | ```c# 169 | // Without filter example 170 | SpList listA = myUT.GetSpListByTitle("LISTNAMEA"); 171 | SpItemCollection collection = myUT.GetSpItemCollection(listA); 172 | 173 | // With filter example 174 | SpList listB = myUT.GetSpListByTitle("LISTNAMEB"); 175 | SpItemCollection collection = myUT.GetSpItemCollection(listB,"$filter=Fieldname eq 'Whatever'"); 176 | ``` 177 | ### Access Field Values 178 | ```c# 179 | int yourItemId = 100; 180 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 181 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 182 | string title = item.Data["Title"]; 183 | ``` 184 | ### Update SpItem 185 | ```c# 186 | int yourItemId = 100; 187 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 188 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 189 | item.SetFieldValue("Title","New Title"); 190 | myUT.UpdateSpItem(item,list); 191 | ``` 192 | ### Setting Lookups, User and URL Fields 193 | ```c# 194 | int yourItemId = 100; 195 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 196 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 197 | 198 | // Set LookupSingle and UserSingle 199 | item.SetFieldValue("SingleId","100"); // "Id" must be applied at the end of the Fieldname! 200 | item.SetFieldValue("SingleId","-1"); // Resets the Field to empty 201 | 202 | // Set LookupMulti and UserMulti 203 | item.SetFieldValue("MultiId","{'results':[100,101]}"); // "Id" must be applied at the end of the Fieldname! 204 | item.SetFieldValue("MultiId","{'results':[]}"); // Resets the Field to empty 205 | 206 | // Set URL Field 207 | item.SetFieldValue("UrlField","{'Url':'https://github.com','Description':'GitHub'}"); 208 | item.SetFieldValue("UrlField","{'Url':'','Description':''}"); // Resets the Field to empty 209 | 210 | myUT.UpdateSpItem(item,list); 211 | ``` 212 | ### Delete SpItem 213 | ```c# 214 | int yourItemId = 100; 215 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 216 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 217 | myUT.DeleteSpItem(item,list); 218 | ``` 219 | ### Get an Attachment from a SpItem by Filename 220 | ```c# 221 | int yourItemId = 100; 222 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 223 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 224 | SpFile attachment = myUT.GetAttachmentFromSpItem("FILENAME.txt",item,list); 225 | ``` 226 | ### Get all Attachments from a SpItem 227 | ```c# 228 | int yourItemId = 100; 229 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 230 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 231 | SpFileCollection attachments = myUT.GetAllAttachmentsFromSpItem(item,list); 232 | ``` 233 | ### Upload an Attachment to a SpItem 234 | ```c# 235 | int yourItemId = 100; 236 | string path = @"C:\yourpath\FILENAME.txt"; 237 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 238 | SpItem item = myUT.GetSpItemByID(yourItemId,list); 239 | SpFile attachment = myUT.UploadSpItemAttachment(path,item,list); 240 | ``` 241 | ### Rename an Attachment of a SpItem 242 | ```c# 243 | SpList list = ut.GetSpListByTitle("LISTNAME"); 244 | SpItem item = ut.GetSpItemByID(1, list); 245 | SpFile file = ut.GetAttachmentFromSpItem("FILENAME.TYPE", item, list); 246 | ut.RenameSpItemAttachment("NEWFILENAME.TYPE", file); 247 | ``` 248 | ## Field Utilities 249 | ### Get a SpField by InternalName or Title 250 | ```c# 251 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 252 | SpField field = myUT.GetSpFieldByInternalNameOrTitle("InterNameOrTitle",list); 253 | ``` 254 | ### Get a SpField by ID 255 | ```c# 256 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 257 | SpField field = myUT.GetSpFieldByID("GUID",list); // e.g.: "bacfa614-08de-428e-be54-24d673600901" 258 | ``` 259 | ### Get all SpFields from List 260 | ```c# 261 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 262 | SpFieldCollection fieldCollection = myUT.GetSpFieldsFromList(list); 263 | ``` 264 | ### Create a SpField on a List 265 | ```c# 266 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 267 | SpField field = new SpField(); 268 | field.InternalName = "MyNewField"; 269 | field.SpFieldTypeKind = SpField.TypeKind.Text; 270 | field.SetProperty("SchemaXml",""); 271 | myUT.CreateSpField(field,list); 272 | ``` 273 | ### Update a SpField on a List 274 | ```c# 275 | string newXmlSchema = "SchemaXmlString"; // look above 276 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 277 | SpField field = myUT.GetSpFieldByInternalNameOrTitle("InterNameOrTitle",list); 278 | field.Properties["SchemaXml"] = newXmlSchema; 279 | myUT.UpdateSpField(field,list); 280 | ``` 281 | ### Delete a SpField from a List 282 | ```c# 283 | SpList list = myUT.GetSpListByTitle("LISTNAME"); 284 | SpField field = myUT.GetSpFieldByInternalNameOrTitle("InterNameOrTitle",list); 285 | myUT.DeleteSpField(field,list); 286 | ``` 287 | 288 | ## User Utilities 289 | ### Get SpUser by UserName 290 | ```c# 291 | SpUser user = myUT.GetSpUserByUserName("USERNAME"); 292 | ``` 293 | ### Get SpUser by Id 294 | ```c# 295 | int userId = 100; 296 | SpUser user = myUT.GetSpUserById(userId); 297 | ``` 298 | -------------------------------------------------------------------------------- /SpRestUtility/SpRestUtilities.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Net; 4 | using System.IO; 5 | using System.Xml; 6 | using System.Collections.Generic; 7 | 8 | namespace SpRestUtility 9 | { 10 | public class SpRestUtilities 11 | { 12 | #region Properties 13 | private string _siteUrl; 14 | public string SiteUrl 15 | { 16 | get 17 | { 18 | return _siteUrl; 19 | } 20 | set 21 | { 22 | // Fix given SiteUrl 23 | _siteUrl = (!value.EndsWith("/")) ? value += "/" : value; 24 | } 25 | } 26 | public NetworkCredential Credentials { get; set; } 27 | private string _formDigest { get; set; } 28 | #endregion 29 | 30 | #region All Utilities 31 | #region List Utilities 32 | public SpList GetSpListByTitle(string title) 33 | { 34 | try 35 | { 36 | SpList foundList = new SpList(); 37 | XmlDocument listXml = new XmlDocument(); 38 | listXml = GetListXMLByTitle(title); 39 | foundList = ReturnListByXML(listXml); 40 | foundList.SpListTemplateType = (SpListTemplateType)int.Parse(foundList.Properties["BaseTemplate"]); 41 | return foundList; 42 | } 43 | catch (Exception ex) 44 | { 45 | throw ex; 46 | } 47 | } 48 | 49 | public SpList TryGetSpListByTitle(string title) 50 | { 51 | try 52 | { 53 | SpList list = GetSpListByTitle(title); 54 | return list; 55 | } 56 | catch(Exception ex) 57 | { 58 | return null; 59 | } 60 | } 61 | 62 | public SpList GetSpListByID(string listID) 63 | { 64 | try 65 | { 66 | SpList foundList = new SpList(); 67 | XmlDocument listXml = new XmlDocument(); 68 | listXml = GetListXMLByGuid(listID); 69 | foundList = ReturnListByXML(listXml); 70 | foundList.SpListTemplateType = (SpListTemplateType)int.Parse(foundList.Properties["BaseTemplate"]); 71 | return foundList; 72 | } 73 | catch (Exception ex) 74 | { 75 | throw ex; 76 | } 77 | } 78 | 79 | public SpList CreateSpList(SpList list) 80 | { 81 | try 82 | { 83 | GetAndSetFormDigest(); 84 | list.SetProperty("BaseTemplate", ((int)list.SpListTemplateType).ToString()); 85 | string listDataString = ReturnDataStringForRestRequest(list); 86 | 87 | string postBody = "{'__metadata':{'type':'SP.List'}, " + listDataString + "}"; 88 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 89 | postBody = Encoding.UTF8.GetString(postData); 90 | 91 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists"); 92 | restRequest.Method = "POST"; 93 | restRequest.Credentials = Credentials; 94 | restRequest.ContentType = "application/json;odata=verbose"; 95 | restRequest.Accept = "application/atom+xml"; 96 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 97 | 98 | Stream restRequestStream = restRequest.GetRequestStream(); 99 | restRequestStream.Write(postData, 0, postData.Length); 100 | restRequestStream.Close(); 101 | 102 | XmlDocument responseXmlDoc = new XmlDocument(); 103 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 104 | StreamReader streamReader = new StreamReader(restResponse.GetResponseStream()); 105 | responseXmlDoc.LoadXml(streamReader.ReadToEnd()); 106 | 107 | return ReturnListByXML(responseXmlDoc); 108 | } 109 | catch (Exception ex) 110 | { 111 | throw ex; 112 | } 113 | } 114 | 115 | public HttpWebResponse UpdateSpList(SpList list) 116 | { 117 | try 118 | { 119 | GetAndSetFormDigest(); 120 | string listDataString = ReturnDataStringForRestRequest(list); 121 | 122 | string listPostBody = "{'__metadata':{'type':'SP.List'}, " + listDataString + "}"; 123 | byte[] listPostData = Encoding.UTF8.GetBytes(listPostBody); 124 | listPostBody = Encoding.UTF8.GetString(listPostData); 125 | 126 | HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')"); 127 | listRequest.Method = "POST"; 128 | listRequest.Credentials = Credentials; 129 | listRequest.Accept = "application/atom+xml"; 130 | listRequest.ContentType = "application/json;odata=verbose"; 131 | listRequest.Headers.Add("X-HTTP-Method", "MERGE"); 132 | listRequest.Headers.Add("IF-MATCH", "*"); 133 | listRequest.Headers.Add("X-RequestDigest", _formDigest); 134 | 135 | Stream listRequestStream = listRequest.GetRequestStream(); 136 | listRequestStream.Write(listPostData, 0, listPostData.Length); 137 | listRequestStream.Close(); 138 | 139 | HttpWebResponse itemResponse = (HttpWebResponse)listRequest.GetResponse(); 140 | return itemResponse; 141 | } 142 | catch (Exception ex) 143 | { 144 | throw ex; 145 | } 146 | } 147 | 148 | public HttpWebResponse DeleteSpList(SpList list) 149 | { 150 | try 151 | { 152 | GetAndSetFormDigest(); 153 | 154 | HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')"); 155 | listRequest.Method = "POST"; 156 | listRequest.Credentials = Credentials; 157 | listRequest.Accept = "application/atom+xml"; 158 | listRequest.ContentType = "application/json;odata=verbose"; 159 | listRequest.ContentLength = 0; 160 | listRequest.Headers.Add("X-HTTP-Method", "DELETE"); 161 | listRequest.Headers.Add("IF-MATCH", "*"); 162 | listRequest.Headers.Add("X-RequestDigest", _formDigest); 163 | 164 | HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse(); 165 | return listResponse; 166 | } 167 | catch (Exception ex) 168 | { 169 | throw ex; 170 | } 171 | } 172 | 173 | private XmlDocument GetListXMLByTitle(string title) 174 | { 175 | try 176 | { 177 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists/GetByTitle('" + title + "')"); 178 | restRequest.Method = "GET"; 179 | restRequest.Credentials = Credentials; 180 | restRequest.Accept = "application/atom+xml"; 181 | restRequest.ContentType = "application/atom+xml;type=entry"; 182 | 183 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 184 | 185 | XmlDocument responseXmlDoc = new XmlDocument(); 186 | StreamReader listReader = new StreamReader(restResponse.GetResponseStream()); 187 | responseXmlDoc.LoadXml(listReader.ReadToEnd()); 188 | 189 | return responseXmlDoc; 190 | } 191 | catch (Exception ex) 192 | { 193 | throw ex; 194 | } 195 | } 196 | 197 | private XmlDocument GetListXMLByGuid(string guid) 198 | { 199 | try 200 | { 201 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + guid + "')"); 202 | restRequest.Method = "GET"; 203 | restRequest.Credentials = Credentials; 204 | restRequest.Accept = "application/atom+xml"; 205 | restRequest.ContentType = "application/atom+xml;type=entry"; 206 | 207 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 208 | 209 | XmlDocument responseXmlDoc = new XmlDocument(); 210 | StreamReader listReader = new StreamReader(restResponse.GetResponseStream()); 211 | responseXmlDoc.LoadXml(listReader.ReadToEnd()); 212 | 213 | return responseXmlDoc; 214 | } 215 | catch (Exception ex) 216 | { 217 | throw ex; 218 | } 219 | } 220 | 221 | private SpList ReturnListByXML(XmlDocument listXmlDoc) 222 | { 223 | try 224 | { 225 | XmlNamespaceManager lManager = ReturnSpXmlNameSpaceManager(listXmlDoc); 226 | 227 | XmlNode listIdNode = listXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Id", lManager); 228 | XmlNodeList propertyNodes = listXmlDoc.SelectNodes("//atom:entry/atom:content/m:properties", lManager); 229 | 230 | SpList list = new SpList(); 231 | list.Id = listIdNode.InnerText; 232 | foreach (XmlNode node in propertyNodes) 233 | { 234 | foreach (XmlNode childNode in node.ChildNodes) 235 | { 236 | list.SetProperty(childNode.Name.Replace("d:", ""), childNode.InnerText); 237 | } 238 | } 239 | 240 | return list; 241 | } 242 | catch (Exception ex) 243 | { 244 | throw ex; 245 | } 246 | } 247 | #endregion 248 | 249 | #region Library Utilities 250 | public SpFolder GetSpFolderByPath(string folderPath, SpList list = null) 251 | { 252 | try 253 | { 254 | if (list != null) 255 | folderPath = list.Properties["Title"] + FixedPath(folderPath); 256 | 257 | HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folderPath + "')"); 258 | listRequest.Method = "GET"; 259 | listRequest.Credentials = Credentials; 260 | listRequest.Accept = "application/atom+xml"; 261 | 262 | HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse(); 263 | 264 | XmlDocument responseXmlDoc = new XmlDocument(); 265 | StreamReader listReader = new StreamReader(listResponse.GetResponseStream()); 266 | responseXmlDoc.LoadXml(listReader.ReadToEnd()); 267 | 268 | return ReturnFolderByXML(responseXmlDoc); 269 | } 270 | catch (Exception ex) 271 | { 272 | throw ex; 273 | } 274 | } 275 | 276 | public SpFolderCollection GetSpFolderCollectionByPath(string folderPath, SpList list = null) 277 | { 278 | try 279 | { 280 | if (list != null) 281 | folderPath = list.Properties["Title"] + FixedPath(folderPath); 282 | 283 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folderPath + "')/Folders"); 284 | restRequest.Method = "GET"; 285 | restRequest.Credentials = Credentials; 286 | restRequest.Accept = "application/atom+xml"; 287 | 288 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 289 | 290 | XmlDocument responseXmlDoc = new XmlDocument(); 291 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 292 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 293 | 294 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 295 | XmlNodeList responseNodes = responseXmlDoc.SelectNodes("//atom:entry", iManager); 296 | 297 | SpFolderCollection folderCollection = new SpFolderCollection(); 298 | foreach (XmlNode node in responseNodes) 299 | { 300 | XmlDocument folderXmlDoc = new XmlDocument(); 301 | folderXmlDoc.LoadXml(node.OuterXml); 302 | SpFolder folder = ReturnFolderByXML(folderXmlDoc); 303 | folderCollection.Folders.Add(ReturnFolderByXML(folderXmlDoc)); 304 | } 305 | 306 | if (folderCollection.Folders.Count > 0) 307 | folderCollection.Folders.RemoveAt(0); 308 | 309 | return folderCollection; 310 | } 311 | catch (Exception ex) 312 | { 313 | throw ex; 314 | } 315 | } 316 | 317 | public SpFolderCollection GetAllSpFoldersFromSpFolder(SpFolder folder) 318 | { 319 | try 320 | { 321 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')/Folders"); 322 | restRequest.Method = "GET"; 323 | restRequest.Credentials = Credentials; 324 | restRequest.Accept = "application/atom+xml"; 325 | 326 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 327 | 328 | XmlDocument responseXmlDoc = new XmlDocument(); 329 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 330 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 331 | 332 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 333 | XmlNodeList responseNodes = responseXmlDoc.SelectNodes("//atom:entry", iManager); 334 | 335 | SpFolderCollection folderCollection = new SpFolderCollection(); 336 | foreach (XmlNode node in responseNodes) 337 | { 338 | XmlDocument folderXmlDoc = new XmlDocument(); 339 | folderXmlDoc.LoadXml(node.OuterXml); 340 | folderCollection.Folders.Add(ReturnFolderByXML(folderXmlDoc)); 341 | } 342 | 343 | if (folderCollection.Folders.Count > 0) 344 | folderCollection.Folders.RemoveAt(0); 345 | 346 | return folderCollection; 347 | } 348 | catch (Exception ex) 349 | { 350 | throw ex; 351 | } 352 | } 353 | 354 | public SpFolder CreateSpFolder(string folderPath, SpList list = null) 355 | { 356 | try 357 | { 358 | GetAndSetFormDigest(); 359 | 360 | if (list != null) 361 | folderPath = list.Properties["Title"] + FixedPath(folderPath); 362 | 363 | string postBody = "{'__metadata': {'type': 'SP.Folder'}, 'ServerRelativeUrl': '" + folderPath + "'}"; 364 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 365 | postBody = Encoding.UTF8.GetString(postData); 366 | 367 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/folders"); 368 | restRequest.Method = "POST"; 369 | restRequest.Credentials = Credentials; 370 | restRequest.ContentLength = postData.Length; 371 | restRequest.ContentType = "application/json;odata=verbose"; 372 | restRequest.Accept = "application/atom+xml"; 373 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 374 | 375 | Stream responseReader = restRequest.GetRequestStream(); 376 | responseReader.Write(postData, 0, postData.Length); 377 | responseReader.Close(); 378 | 379 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 380 | 381 | XmlDocument responseXmlDoc = new XmlDocument(); 382 | StreamReader streamReader = new StreamReader(restResponse.GetResponseStream()); 383 | responseXmlDoc.LoadXml(streamReader.ReadToEnd()); 384 | 385 | return ReturnFolderByXML(responseXmlDoc); 386 | } 387 | catch (Exception ex) 388 | { 389 | throw ex; 390 | } 391 | } 392 | 393 | // Somehow this isn't working! 394 | private HttpWebResponse RenameSpFolder(string newName, SpFolder folder) 395 | { 396 | try 397 | { 398 | GetAndSetFormDigest(); 399 | 400 | string postBody = "{'__metadata': {'type': 'SP.Folder'}, 'Name': '" + newName + "'}"; 401 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 402 | postBody = Encoding.UTF8.GetString(postData); 403 | 404 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')"); 405 | restRequest.Method = "POST"; 406 | restRequest.Credentials = Credentials; 407 | restRequest.ContentLength = postData.Length; 408 | restRequest.Accept = "application/atom+xml"; 409 | restRequest.ContentType = "application/json;odata=verbose"; 410 | restRequest.Headers.Add("X-HTTP-Method", "MERGE"); 411 | restRequest.Headers.Add("IF-MATCH", "*"); 412 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 413 | 414 | Stream requestStream = restRequest.GetRequestStream(); 415 | requestStream.Write(postData, 0, postData.Length); 416 | requestStream.Close(); 417 | 418 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 419 | return restResponse; 420 | } 421 | catch (Exception ex) 422 | { 423 | throw ex; 424 | } 425 | } 426 | 427 | public HttpWebResponse DeleteSpFolder(SpFolder folder) 428 | { 429 | try 430 | { 431 | GetAndSetFormDigest(); 432 | 433 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')"); 434 | restRequest.Method = "POST"; 435 | restRequest.Credentials = Credentials; 436 | restRequest.ContentLength = 0; 437 | restRequest.Headers.Add("X-HTTP-Method", "DELETE"); 438 | restRequest.Headers.Add("IF-MATCH", "*"); 439 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 440 | 441 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 442 | return restResponse; 443 | } 444 | catch (Exception ex) 445 | { 446 | throw ex; 447 | } 448 | } 449 | 450 | public SpFile GetSpFileByFilenameFromSpFolder(string fileName, SpFolder folder) 451 | { 452 | try 453 | { 454 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')/Files('" + fileName + "')"); 455 | restRequest.Method = "GET"; 456 | restRequest.Credentials = Credentials; 457 | restRequest.Accept = "application/atom+xml"; 458 | 459 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 460 | 461 | XmlDocument responseXmlDoc = new XmlDocument(); 462 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 463 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 464 | 465 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 466 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:entry", iManager); 467 | XmlDocument responseNodeXmlDoc = new XmlDocument(); 468 | responseNodeXmlDoc.LoadXml(responseNode.OuterXml); 469 | 470 | return ReturnFileByXML(responseNodeXmlDoc); 471 | } 472 | catch (Exception ex) 473 | { 474 | throw ex; 475 | } 476 | } 477 | 478 | public SpFileCollection GetAllSpFilesFromSpFolder(SpFolder folder) 479 | { 480 | try 481 | { 482 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')/Files"); 483 | restRequest.Method = "GET"; 484 | restRequest.Credentials = Credentials; 485 | restRequest.Accept = "application/atom+xml"; 486 | 487 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 488 | 489 | XmlDocument responseXmlDoc = new XmlDocument(); 490 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 491 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 492 | 493 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 494 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:feed", iManager); 495 | 496 | XmlDocument responseNodeXmlDoc = new XmlDocument(); 497 | responseNodeXmlDoc.LoadXml(responseNode.OuterXml); 498 | XmlNodeList fileNodes = responseNodeXmlDoc.SelectNodes("//atom:entry", iManager); 499 | 500 | SpFileCollection fileCollection = new SpFileCollection(); 501 | foreach (XmlNode node in fileNodes) 502 | { 503 | XmlDocument fileXmlDoc = new XmlDocument(); 504 | fileXmlDoc.LoadXml(node.OuterXml); 505 | SpFile file = ReturnFileByXML(fileXmlDoc); 506 | fileCollection.Files.Add(file); 507 | } 508 | 509 | return fileCollection; 510 | } 511 | catch (Exception ex) 512 | { 513 | throw ex; 514 | } 515 | } 516 | 517 | public SpFile UploadSpFileToSpFolder(string filePath, SpFolder folder, bool overwrite = true) 518 | { 519 | try 520 | { 521 | GetAndSetFormDigest(); 522 | 523 | string fileName = Path.GetFileName(filePath); 524 | byte[] postBody = File.ReadAllBytes(filePath); 525 | string url = _siteUrl + "_api/web/GetFolderByServerRelativeUrl('" + folder.Properties["ServerRelativeUrl"] + "')/Files/add(url='" + fileName + "',overwrite=" + overwrite.ToString().ToLower() + ")"; 526 | 527 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(url); 528 | restRequest.Method = "POST"; 529 | restRequest.Credentials = Credentials; 530 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 531 | 532 | Stream requestStream = restRequest.GetRequestStream(); 533 | requestStream.Write(postBody, 0, postBody.Length); 534 | requestStream.Close(); 535 | 536 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 537 | 538 | XmlDocument responseXmlDoc = new XmlDocument(); 539 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 540 | responseXmlDoc.Load(responseReader); 541 | 542 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 543 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:entry", iManager); 544 | XmlDocument fileXmlDoc = new XmlDocument(); 545 | fileXmlDoc.LoadXml(responseNode.OuterXml); 546 | 547 | return ReturnFileByXML(fileXmlDoc); 548 | } 549 | catch (Exception ex) 550 | { 551 | throw ex; 552 | } 553 | } 554 | 555 | public HttpWebResponse DeleteSpFile(SpFile file) 556 | { 557 | try 558 | { 559 | GetAndSetFormDigest(); 560 | 561 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFileByServerRelativeUrl('" + file.Properties["ServerRelativeUrl"] + "')"); 562 | restRequest.Method = "POST"; 563 | restRequest.Credentials = Credentials; 564 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 565 | restRequest.ContentLength = 0; 566 | restRequest.Headers.Add("X-HTTP-Method", "DELETE"); 567 | restRequest.Headers.Add("IF-MATCH", "*"); 568 | 569 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 570 | return restResponse; 571 | } 572 | catch (Exception ex) 573 | { 574 | throw ex; 575 | } 576 | } 577 | 578 | public HttpWebResponse MoveSpFile(SpFile file, SpFolder destinationFolder) 579 | { 580 | try 581 | { 582 | GetAndSetFormDigest(); 583 | 584 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFileByServerRelativeUrl('" + file.Properties["ServerRelativeUrl"] + "')/moveto(newurl='" + destinationFolder.Properties["ServerRelativeUrl"] + "/" + file.Properties["Name"] + "',flags=1)"); 585 | restRequest.Method = "POST"; 586 | restRequest.Credentials = Credentials; 587 | restRequest.ContentLength = 0; 588 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 589 | 590 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 591 | return restResponse; 592 | } 593 | catch (Exception ex) 594 | { 595 | throw ex; 596 | } 597 | } 598 | 599 | public HttpWebResponse CopySpFile(SpFile file, SpFolder destinationFolder, bool overwrite = true) 600 | { 601 | try 602 | { 603 | GetAndSetFormDigest(); 604 | 605 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/GetFileByServerRelativeUrl('" + file.Properties["ServerRelativeUrl"] + "')/copyto(strnewurl='" + destinationFolder.Properties["ServerRelativeUrl"] + "/" + file.Properties["Name"] + "',boverwrite=" + overwrite.ToString().ToLower() + ")"); 606 | restRequest.Method = "POST"; 607 | restRequest.Credentials = Credentials; 608 | restRequest.ContentLength = 0; 609 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 610 | 611 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 612 | return restResponse; 613 | } 614 | catch (Exception ex) 615 | { 616 | throw ex; 617 | } 618 | } 619 | 620 | private SpFolder ReturnFolderByXML(XmlDocument folderXmlDoc) 621 | { 622 | try 623 | { 624 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(folderXmlDoc); 625 | XmlNodeList propertyNodes = folderXmlDoc.SelectNodes("//atom:content/m:properties", iManager); 626 | 627 | SpFolder folder = new SpFolder(); 628 | foreach (XmlNode node in propertyNodes) 629 | { 630 | foreach (XmlNode childNode in node.ChildNodes) 631 | { 632 | folder.SetProperty(childNode.Name.Replace("d:", ""), childNode.InnerText); 633 | } 634 | } 635 | 636 | return folder; 637 | } 638 | catch (Exception ex) 639 | { 640 | throw ex; 641 | } 642 | } 643 | 644 | private SpFile ReturnFileByXML(XmlDocument fileXmlDoc) 645 | { 646 | try 647 | { 648 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(fileXmlDoc); 649 | XmlNodeList propertyNodes = fileXmlDoc.SelectNodes("//atom:content/m:properties", iManager); 650 | 651 | SpFile file = new SpFile(); 652 | foreach (XmlNode node in propertyNodes) 653 | { 654 | foreach (XmlNode childNode in node.ChildNodes) 655 | { 656 | file.SetProperty(childNode.Name.Replace("d:", ""), childNode.InnerText); 657 | } 658 | } 659 | 660 | return file; 661 | } 662 | catch (Exception ex) 663 | { 664 | throw ex; 665 | } 666 | } 667 | #endregion 668 | 669 | #region Field Utilities 670 | public SpField GetSpFieldByInternalNameOrTitle(string interNalnameOrTitle, SpList list) 671 | { 672 | try 673 | { 674 | XmlDocument fieldXmlDoc = new XmlDocument(); 675 | 676 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields/getbyinternalnameortitle('" + interNalnameOrTitle + "')"); 677 | restRequest.Method = "GET"; 678 | restRequest.Credentials = Credentials; 679 | restRequest.Accept = "application/atom+xml"; 680 | 681 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 682 | 683 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 684 | 685 | fieldXmlDoc.LoadXml(responseReader.ReadToEnd()); 686 | 687 | return ReturnFieldByXML(fieldXmlDoc); 688 | } 689 | catch (Exception ex) 690 | { 691 | throw ex; 692 | } 693 | } 694 | 695 | public SpField GetSpFieldByID(string fieldID, SpList list) 696 | { 697 | try 698 | { 699 | XmlDocument fieldXmlDoc = new XmlDocument(); 700 | 701 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields('" + fieldID + "')"); 702 | restRequest.Method = "GET"; 703 | restRequest.Credentials = Credentials; 704 | restRequest.Accept = "application/atom+xml"; 705 | 706 | HttpWebResponse fieldResponse = (HttpWebResponse)restRequest.GetResponse(); 707 | 708 | StreamReader responseReader = new StreamReader(fieldResponse.GetResponseStream()); 709 | 710 | fieldXmlDoc.LoadXml(responseReader.ReadToEnd()); 711 | 712 | return ReturnFieldByXML(fieldXmlDoc); 713 | } 714 | catch (Exception ex) 715 | { 716 | throw ex; 717 | } 718 | } 719 | 720 | public SpFieldCollection GetSpFieldsFromList(SpList list) 721 | { 722 | try 723 | { 724 | SpFieldCollection fieldColl = new SpFieldCollection(); 725 | XmlDocument fieldsXmlDoc = new XmlDocument(); 726 | 727 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields"); 728 | restRequest.Method = "GET"; 729 | restRequest.Credentials = Credentials; 730 | restRequest.Accept = "application/atom+xml"; 731 | 732 | HttpWebResponse fieldsResponse = (HttpWebResponse)restRequest.GetResponse(); 733 | 734 | StreamReader responseReader = new StreamReader(fieldsResponse.GetResponseStream()); 735 | 736 | fieldsXmlDoc.LoadXml(responseReader.ReadToEnd()); 737 | XmlNamespaceManager fManager = ReturnSpXmlNameSpaceManager(fieldsXmlDoc); 738 | XmlNodeList fieldsNodes = fieldsXmlDoc.SelectNodes("//atom:entry", fManager); 739 | 740 | foreach (XmlNode node in fieldsNodes) 741 | { 742 | XmlDocument fieldXmlDoc = new XmlDocument(); 743 | fieldXmlDoc.LoadXml(node.OuterXml); 744 | fieldColl.Fields.Add(ReturnFieldByXML(fieldXmlDoc)); 745 | } 746 | 747 | return fieldColl; 748 | } 749 | catch (Exception ex) 750 | { 751 | throw ex; 752 | } 753 | } 754 | 755 | public SpField CreateSpField(SpField field, SpList list) 756 | { 757 | try 758 | { 759 | GetAndSetFormDigest(); 760 | string fieldXmlString = field.Properties["SchemaXml"]; 761 | 762 | string postBody = "{'__metadata': {'type': 'SP.Field'}, 'FieldTypeKind': " + (int)field.FieldTypeKind + ", 'Title': '" + field.InternalName + "', 'SchemaXml': '" + fieldXmlString + "'}"; 763 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 764 | postBody = Encoding.UTF8.GetString(postData); 765 | 766 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields"); 767 | restRequest.Method = "POST"; 768 | restRequest.Credentials = Credentials; 769 | restRequest.ContentLength = postData.Length; 770 | restRequest.ContentType = "application/json;odata=verbose"; 771 | restRequest.Accept = "application/atom+xml"; 772 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 773 | 774 | Stream responseReader = restRequest.GetRequestStream(); 775 | responseReader.Write(postData, 0, postData.Length); 776 | responseReader.Close(); 777 | 778 | XmlDocument responseXmlDoc = new XmlDocument(); 779 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 780 | StreamReader streamReader = new StreamReader(restResponse.GetResponseStream()); 781 | responseXmlDoc.LoadXml(streamReader.ReadToEnd()); 782 | 783 | return ReturnFieldByXML(responseXmlDoc); 784 | } 785 | catch (Exception ex) 786 | { 787 | throw ex; 788 | } 789 | } 790 | 791 | public HttpWebResponse UpdateSpField(SpField field, SpList list) 792 | { 793 | GetAndSetFormDigest(); 794 | string fieldXmlString = field.Properties["SchemaXml"]; 795 | 796 | string postBody = "{'__metadata': {'type': 'SP.Field'}, 'FieldTypeKind': " + (int)field.FieldTypeKind + ", 'Title': '" + field.InternalName + "', 'SchemaXml': '" + fieldXmlString + "'}"; 797 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 798 | postBody = Encoding.UTF8.GetString(postData); 799 | 800 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields('" + field.Id + "')"); 801 | restRequest.Method = "POST"; 802 | restRequest.Credentials = Credentials; 803 | restRequest.ContentLength = postData.Length; 804 | restRequest.ContentType = "application/json;odata=verbose"; 805 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 806 | restRequest.Headers.Add("X-HTTP-Method", "MERGE"); 807 | 808 | Stream restRequestStream = restRequest.GetRequestStream(); 809 | restRequestStream.Write(postData, 0, postData.Length); 810 | restRequestStream.Close(); 811 | 812 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 813 | return restResponse; 814 | } 815 | 816 | public HttpWebResponse DeleteSpField(SpField field, SpList list) 817 | { 818 | try 819 | { 820 | GetAndSetFormDigest(); 821 | 822 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/fields('" + field.Id + "')"); 823 | restRequest.Method = "POST"; 824 | restRequest.Credentials = Credentials; 825 | restRequest.ContentLength = 0; 826 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 827 | restRequest.Headers.Add("X-HTTP-Method", "DELETE"); 828 | 829 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 830 | return restResponse; 831 | } 832 | catch (Exception ex) 833 | { 834 | throw ex; 835 | } 836 | } 837 | 838 | private SpField ReturnFieldByXML(XmlDocument fieldXmlDoc) 839 | { 840 | try 841 | { 842 | XmlNamespaceManager fManager = ReturnSpXmlNameSpaceManager(fieldXmlDoc); 843 | 844 | XmlNode fieldIdNode = fieldXmlDoc.SelectSingleNode("//atom:content/m:properties/d:Id", fManager); 845 | XmlNode fieldTypeKindXmlNode = fieldXmlDoc.SelectSingleNode("//atom:content/m:properties/d:FieldTypeKind", fManager); 846 | XmlNode fieldInternalNameNode = fieldXmlDoc.SelectSingleNode("//atom:content/m:properties/d:InternalName", fManager); 847 | XmlNodeList propertyNodes = fieldXmlDoc.SelectNodes("//atom:content/m:properties", fManager); 848 | 849 | SpField field = new SpField(); 850 | field.Id = fieldIdNode.InnerText; 851 | field.InternalName = fieldInternalNameNode.InnerText; 852 | field.FieldTypeKind = (SpFieldTypeKind)Int32.Parse(fieldTypeKindXmlNode.InnerText); 853 | foreach (XmlNode node in propertyNodes) 854 | { 855 | foreach (XmlNode childNode in node.ChildNodes) 856 | { 857 | field.SetProperty(childNode.Name.Replace("d:", ""), childNode.InnerText); 858 | } 859 | } 860 | 861 | return field; 862 | } 863 | catch (Exception ex) 864 | { 865 | throw ex; 866 | } 867 | } 868 | #endregion 869 | 870 | #region ListItem Utilities 871 | public SpItem GetSpItemByID(int itemID, SpList list) 872 | { 873 | try 874 | { 875 | XmlDocument responseXmlDoc = new XmlDocument(); 876 | XmlDocument itemEntryXmlDoc = new XmlDocument(); 877 | 878 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + itemID.ToString() + ")"); 879 | restRequest.Method = "GET"; 880 | restRequest.Credentials = Credentials; 881 | restRequest.Accept = "application/atom+xml"; 882 | 883 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 884 | 885 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 886 | responseXmlDoc.Load(responseReader); 887 | 888 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 889 | XmlNode itemNode = responseXmlDoc.SelectSingleNode("//atom:entry", iManager); 890 | itemEntryXmlDoc.LoadXml(itemNode.OuterXml); 891 | 892 | return ReturnItemByXML(itemEntryXmlDoc); 893 | } 894 | catch (Exception ex) 895 | { 896 | throw ex; 897 | } 898 | } 899 | 900 | public SpItemCollection GetSpItemCollection(SpList list, string filter = "") 901 | { 902 | try 903 | { 904 | HttpWebRequest itemRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items?" + filter); 905 | itemRequest.Method = "GET"; 906 | itemRequest.Credentials = Credentials; 907 | itemRequest.Accept = "application/atom+xml"; 908 | 909 | HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse(); 910 | 911 | XmlDocument itemsXmlDoc = new XmlDocument(); 912 | StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream()); 913 | itemsXmlDoc.Load(itemReader); 914 | 915 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(itemsXmlDoc); 916 | XmlNodeList itemNodes = itemsXmlDoc.SelectNodes("//atom:entry", iManager); 917 | 918 | SpItemCollection itemCollection = new SpItemCollection(); 919 | foreach (XmlNode node in itemNodes) 920 | { 921 | XmlDocument itemXmlDoc = new XmlDocument(); 922 | itemXmlDoc.LoadXml(node.OuterXml); 923 | itemCollection.Items.Add(ReturnItemByXML(itemXmlDoc)); 924 | } 925 | 926 | return itemCollection; 927 | } 928 | catch (Exception ex) 929 | { 930 | throw ex; 931 | } 932 | } 933 | 934 | public SpItem CreateSpItem(SpItem item, SpList list) 935 | { 936 | try 937 | { 938 | 939 | GetAndSetFormDigest(); 940 | string itemDataString = ReturnDataStringForRestRequest(item); 941 | 942 | string postBody = "{'__metadata':{'type':'" + list.Properties["ListItemEntityTypeFullName"] + "'}, " + itemDataString + "}"; 943 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 944 | postBody = Encoding.UTF8.GetString(postData); 945 | 946 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items"); 947 | restRequest.Method = "POST"; 948 | restRequest.Credentials = Credentials; 949 | restRequest.ContentLength = postData.Length; 950 | restRequest.ContentType = "application/json;odata=verbose"; 951 | restRequest.Accept = "application/atom+xml"; 952 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 953 | 954 | Stream requestStream = restRequest.GetRequestStream(); 955 | requestStream.Write(postData, 0, postData.Length); 956 | requestStream.Close(); 957 | 958 | XmlDocument responseXmlDoc = new XmlDocument(); 959 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 960 | StreamReader streamReader = new StreamReader(restResponse.GetResponseStream()); 961 | responseXmlDoc.LoadXml(streamReader.ReadToEnd()); 962 | XmlNamespaceManager xmlNameSpaceManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 963 | XmlNode itemNode = responseXmlDoc.SelectSingleNode("//atom:entry", xmlNameSpaceManager); 964 | responseXmlDoc.LoadXml(itemNode.OuterXml); 965 | 966 | return ReturnItemByXML(responseXmlDoc); 967 | } 968 | catch (Exception ex) 969 | { 970 | throw ex; 971 | } 972 | } 973 | 974 | public HttpWebResponse UpdateSpItem(SpItem item, SpList list) 975 | { 976 | try 977 | { 978 | GetAndSetFormDigest(); 979 | string itemDataString = ReturnDataStringForRestRequest(item); 980 | 981 | string postBody = "{'__metadata':{'type':'" + list.Properties["ListItemEntityTypeFullName"] + "'}, " + itemDataString + "}"; 982 | byte[] postData = Encoding.UTF8.GetBytes(postBody); 983 | postBody = Encoding.UTF8.GetString(postData); 984 | 985 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + item.Id.ToString() + ")"); 986 | restRequest.Method = "POST"; 987 | restRequest.Credentials = Credentials; 988 | restRequest.Accept = "application/atom+xml"; 989 | restRequest.ContentType = "application/json;odata=verbose"; 990 | restRequest.Headers.Add("X-HTTP-Method", "MERGE"); 991 | restRequest.Headers.Add("IF-MATCH", "*"); 992 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 993 | 994 | Stream requestStream = restRequest.GetRequestStream(); 995 | requestStream.Write(postData, 0, postData.Length); 996 | requestStream.Close(); 997 | 998 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 999 | return restResponse; 1000 | } 1001 | catch (Exception ex) 1002 | { 1003 | 1004 | throw ex; 1005 | } 1006 | } 1007 | 1008 | public HttpWebResponse DeleteSpItem(SpItem item, SpList list) 1009 | { 1010 | try 1011 | { 1012 | GetAndSetFormDigest(); 1013 | 1014 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + item.Id.ToString() + ")"); 1015 | restRequest.Method = "POST"; 1016 | restRequest.Credentials = Credentials; 1017 | restRequest.Accept = "application/atom+xml"; 1018 | restRequest.ContentType = "application/json;odata=verbose"; 1019 | restRequest.ContentLength = 0; 1020 | restRequest.Headers.Add("X-HTTP-Method", "DELETE"); 1021 | restRequest.Headers.Add("IF-MATCH", "*"); 1022 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 1023 | 1024 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 1025 | return restResponse; 1026 | } 1027 | catch (Exception ex) 1028 | { 1029 | throw ex; 1030 | } 1031 | } 1032 | 1033 | public SpFile GetAttachmentFromSpItem(string fileName, SpItem item, SpList list) 1034 | { 1035 | try 1036 | { 1037 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + item.Id.ToString() + ")/AttachmentFiles('" + fileName + "')"); 1038 | restRequest.Method = "GET"; 1039 | restRequest.Credentials = Credentials; 1040 | restRequest.Accept = "application/atom+xml"; 1041 | 1042 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 1043 | 1044 | XmlDocument responseXmlDoc = new XmlDocument(); 1045 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 1046 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 1047 | 1048 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 1049 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:entry", iManager); 1050 | XmlDocument responseNodeXmlDoc = new XmlDocument(); 1051 | responseNodeXmlDoc.LoadXml(responseNode.OuterXml); 1052 | 1053 | return ReturnFileByXML(responseNodeXmlDoc); 1054 | } 1055 | catch (Exception ex) 1056 | { 1057 | throw ex; 1058 | } 1059 | } 1060 | 1061 | public SpFileCollection GetAllAttachmentsFromSpItem(SpItem item, SpList list) 1062 | { 1063 | try 1064 | { 1065 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + item.Id.ToString() + ")/AttachmentFiles"); 1066 | restRequest.Method = "GET"; 1067 | restRequest.Credentials = Credentials; 1068 | restRequest.Accept = "application/atom+xml"; 1069 | 1070 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 1071 | 1072 | XmlDocument responseXmlDoc = new XmlDocument(); 1073 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 1074 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 1075 | 1076 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 1077 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:feed", iManager); 1078 | 1079 | XmlDocument responseNodeXmlDoc = new XmlDocument(); 1080 | responseNodeXmlDoc.LoadXml(responseNode.OuterXml); 1081 | XmlNodeList fileNodes = responseNodeXmlDoc.SelectNodes("//atom:entry", iManager); 1082 | 1083 | SpFileCollection fileCollection = new SpFileCollection(); 1084 | foreach (XmlNode node in fileNodes) 1085 | { 1086 | XmlDocument fileXmlDoc = new XmlDocument(); 1087 | fileXmlDoc.LoadXml(node.OuterXml); 1088 | SpFile file = ReturnFileByXML(fileXmlDoc); 1089 | fileCollection.Files.Add(file); 1090 | } 1091 | 1092 | return fileCollection; 1093 | } 1094 | catch (Exception ex) 1095 | { 1096 | throw ex; 1097 | } 1098 | } 1099 | 1100 | public SpFile UploadSpItemAttachment(string filePath, SpItem item, SpList list) 1101 | { 1102 | try 1103 | { 1104 | GetAndSetFormDigest(); 1105 | 1106 | string fileName = Path.GetFileName(filePath); 1107 | byte[] postBody = File.ReadAllBytes(filePath); 1108 | 1109 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/lists(guid'" + list.Id + "')/items(" + item.Id.ToString() + ")/AttachmentFiles/add(FileName='" + fileName + "')"); 1110 | restRequest.Method = "POST"; 1111 | restRequest.Credentials = Credentials; 1112 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 1113 | 1114 | Stream requestStream = restRequest.GetRequestStream(); 1115 | requestStream.Write(postBody, 0, postBody.Length); 1116 | requestStream.Close(); 1117 | 1118 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 1119 | 1120 | XmlDocument responseXmlDoc = new XmlDocument(); 1121 | StreamReader responseReader = new StreamReader(restResponse.GetResponseStream()); 1122 | responseXmlDoc.LoadXml(responseReader.ReadToEnd()); 1123 | 1124 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(responseXmlDoc); 1125 | XmlNode responseNode = responseXmlDoc.SelectSingleNode("//atom:entry", iManager); 1126 | XmlDocument responseNodeXmlDoc = new XmlDocument(); 1127 | responseNodeXmlDoc.LoadXml(responseNode.OuterXml); 1128 | 1129 | return ReturnFileByXML(responseNodeXmlDoc); 1130 | } 1131 | catch (Exception ex) 1132 | { 1133 | throw ex; 1134 | } 1135 | } 1136 | 1137 | public HttpWebResponse RenameSpItemAttachment(string newFileName, SpFile file) 1138 | { 1139 | try 1140 | { 1141 | GetAndSetFormDigest(); 1142 | 1143 | string newPath = file.Properties["ServerRelativeUrl"].Replace(file.Properties["FileName"], newFileName); 1144 | 1145 | HttpWebRequest restRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/web/getfilebyserverrelativeurl('" + file.Properties["ServerRelativeUrl"] + "')/moveto(newurl='" + newPath + "', flags=1)"); 1146 | restRequest.Method = "POST"; 1147 | restRequest.Credentials = Credentials; 1148 | restRequest.ContentLength = 0; 1149 | restRequest.Headers.Add("X-HTTP-Method", "PUT"); 1150 | restRequest.Headers.Add("X-RequestDigest", _formDigest); 1151 | 1152 | HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse(); 1153 | return restResponse; 1154 | } 1155 | catch (Exception ex) 1156 | { 1157 | throw ex; 1158 | } 1159 | } 1160 | 1161 | private SpItem ReturnItemByXML(XmlDocument itemXmlDoc) 1162 | { 1163 | try 1164 | { 1165 | XmlNamespaceManager iManager = ReturnSpXmlNameSpaceManager(itemXmlDoc); 1166 | 1167 | XmlNode itemIdNode = itemXmlDoc.SelectSingleNode("//atom:content/m:properties/d:Id", iManager); 1168 | XmlNodeList dataNodes = itemXmlDoc.SelectNodes("//atom:content/m:properties", iManager); 1169 | 1170 | SpItem item = new SpItem(); 1171 | item.Id = int.Parse(itemIdNode.InnerText); 1172 | foreach (XmlNode node in dataNodes) 1173 | { 1174 | foreach (XmlNode childNode in node.ChildNodes) 1175 | { 1176 | XmlDocument outerXmlDoc = new XmlDocument(); 1177 | outerXmlDoc.LoadXml(childNode.OuterXml); 1178 | XmlNode fieldTypeNode = outerXmlDoc.FirstChild; 1179 | string fieldType = (fieldTypeNode.Attributes["m:type"] == null) ? "noType" : fieldTypeNode.Attributes["m:type"].Value; 1180 | 1181 | if (fieldType == "SP.FieldUrlValue") 1182 | { 1183 | XmlNode descriptionNode = fieldTypeNode.SelectSingleNode("d:Description", iManager); 1184 | XmlNode urlNode = fieldTypeNode.SelectSingleNode("d:Url", iManager); 1185 | item.SetFieldValue(childNode.Name.Replace("d:", ""), "{'Url':'" + urlNode.InnerText + "','Description':'" + descriptionNode.InnerText + "'}"); 1186 | } 1187 | else if (fieldType == "Collection(Edm.Int32)") 1188 | { 1189 | XmlNodeList nodeList = fieldTypeNode.SelectNodes("d:element", iManager); 1190 | StringBuilder str = new StringBuilder(); 1191 | str.Append("{'results':["); 1192 | foreach (XmlNode idNode in nodeList) 1193 | { 1194 | str.Append(idNode.InnerText + ","); 1195 | } 1196 | if (nodeList.Count > 0) 1197 | str.Length--; 1198 | str.Append("]}"); 1199 | item.SetFieldValue(childNode.Name.Replace("d:", ""), str.ToString()); 1200 | } 1201 | else 1202 | item.SetFieldValue(childNode.Name.Replace("d:", ""), childNode.InnerText); 1203 | } 1204 | } 1205 | 1206 | return item; 1207 | } 1208 | catch (Exception ex) 1209 | { 1210 | throw ex; 1211 | } 1212 | } 1213 | #endregion 1214 | 1215 | #region User Utilities 1216 | public SpUser GetSpUserByUserName(string userName) 1217 | { 1218 | string url = _siteUrl + "_api/web/siteusers(@v)?@v=%27i%3A0%23.w%7Casbnet%5C" + userName + "%27"; 1219 | try 1220 | { 1221 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 1222 | request.Credentials = Credentials; 1223 | WebResponse response = request.GetResponse(); 1224 | using (Stream responseStream = response.GetResponseStream()) 1225 | { 1226 | using (XmlReader xmlReader = XmlReader.Create(responseStream)) 1227 | { 1228 | XmlDocument userXmlDoc = new XmlDocument(); 1229 | userXmlDoc.Load(xmlReader); 1230 | SpUser spUser = ReturnUserByXML(userXmlDoc); 1231 | return spUser; 1232 | } 1233 | 1234 | } 1235 | } 1236 | catch (Exception ex) 1237 | { 1238 | throw ex; 1239 | } 1240 | } 1241 | 1242 | public SpUser GetSpUserById(int id) 1243 | { 1244 | string url = _siteUrl + "_api/Web/GetUserById(" + id.ToString() + ")"; 1245 | try 1246 | { 1247 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 1248 | request.Credentials = Credentials; 1249 | WebResponse response = request.GetResponse(); 1250 | using (Stream responseStream = response.GetResponseStream()) 1251 | { 1252 | using (XmlReader xmlReader = XmlReader.Create(responseStream)) 1253 | { 1254 | XmlDocument userXmlDoc = new XmlDocument(); 1255 | userXmlDoc.Load(xmlReader); 1256 | SpUser spUser = ReturnUserByXML(userXmlDoc); 1257 | return spUser; 1258 | } 1259 | 1260 | } 1261 | } 1262 | catch (Exception ex) 1263 | { 1264 | throw ex; 1265 | } 1266 | } 1267 | 1268 | public SpUserCollection GetAllSpUsersFromSite() 1269 | { 1270 | string url = _siteUrl + "_api/web/siteusers"; 1271 | SpUserCollection coll = new SpUserCollection(); 1272 | try 1273 | { 1274 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 1275 | request.Credentials = Credentials; 1276 | WebResponse response = request.GetResponse(); 1277 | using (Stream responseStream = response.GetResponseStream()) 1278 | { 1279 | using (XmlReader xmlReader = XmlReader.Create(responseStream)) 1280 | { 1281 | XmlDocument usersXmlDoc = new XmlDocument(); 1282 | usersXmlDoc.Load(xmlReader); 1283 | 1284 | XmlNamespaceManager uManager = ReturnSpXmlNameSpaceManager(usersXmlDoc); 1285 | XmlNodeList userNodes = usersXmlDoc.SelectNodes("//atom:entry", uManager); 1286 | 1287 | foreach (XmlNode userNode in userNodes) 1288 | { 1289 | XmlDocument userXmlDoc = new XmlDocument(); 1290 | userXmlDoc.LoadXml(userNode.OuterXml); 1291 | coll.Users.Add(ReturnUserByXML(userXmlDoc)); 1292 | } 1293 | } 1294 | 1295 | } 1296 | 1297 | return coll; 1298 | } 1299 | catch (Exception ex) 1300 | { 1301 | throw ex; 1302 | } 1303 | 1304 | } 1305 | 1306 | private SpUser ReturnUserByXML(XmlDocument userXmlDoc) 1307 | { 1308 | XmlNamespaceManager uManager = ReturnSpXmlNameSpaceManager(userXmlDoc); 1309 | 1310 | XmlNode uIdNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Id", uManager); 1311 | XmlNode uIsHiddenInUiNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:IsHiddenInUI", uManager); 1312 | XmlNode uLoginNameNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:LoginName", uManager); 1313 | XmlNode uTitleNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Title", uManager); 1314 | XmlNode uPrincipalTypeNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:PrincipalType", uManager); 1315 | XmlNode uEmailNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Email", uManager); 1316 | XmlNode uIsSiteAdminNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:IsSiteAdmin", uManager); 1317 | XmlNode uUserIdNode = userXmlDoc.SelectSingleNode("//atom:entry/atom:content/m:properties/d:UserId", uManager); 1318 | 1319 | SpUser spUser = new SpUser(); 1320 | spUser.Id = Int32.Parse(uIdNode.InnerText); 1321 | spUser.IsHiddenInUi = bool.Parse(uIsHiddenInUiNode.InnerText); 1322 | spUser.LoginName = uLoginNameNode.InnerText; 1323 | spUser.Title = uTitleNode.InnerText; 1324 | spUser.PrincipalType = (SpUserPrincipalType)Int32.Parse(uPrincipalTypeNode.InnerText); 1325 | spUser.Email = uEmailNode.InnerText; 1326 | spUser.IsSiteAdmin = bool.Parse(uIsSiteAdminNode.InnerText); 1327 | spUser.UserId = uUserIdNode.InnerText; 1328 | 1329 | return spUser; 1330 | } 1331 | #endregion 1332 | #endregion 1333 | 1334 | #region Private General Methods 1335 | private void GetAndSetFormDigest() 1336 | { 1337 | try 1338 | { 1339 | HttpWebRequest ctxInfoRequest = (HttpWebRequest)WebRequest.Create(_siteUrl + "_api/contextinfo"); 1340 | ctxInfoRequest.Method = "POST"; 1341 | ctxInfoRequest.ContentType = "text/xml;charset=utf-8"; 1342 | ctxInfoRequest.ContentLength = 0; 1343 | ctxInfoRequest.Credentials = Credentials; 1344 | 1345 | HttpWebResponse ctxInfoResponse = (HttpWebResponse)ctxInfoRequest.GetResponse(); 1346 | 1347 | StreamReader contextinfoReader = new StreamReader(ctxInfoResponse.GetResponseStream(), System.Text.Encoding.UTF8); 1348 | 1349 | XmlDocument formDigestXML = new XmlDocument(); 1350 | formDigestXML.LoadXml(contextinfoReader.ReadToEnd()); 1351 | XmlNamespaceManager fdManager = new XmlNamespaceManager(formDigestXML.NameTable); 1352 | fdManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); 1353 | XmlNode formDigestNode = formDigestXML.SelectSingleNode("//d:FormDigestValue", fdManager); 1354 | _formDigest = formDigestNode.InnerXml; 1355 | } 1356 | catch (Exception ex) 1357 | { 1358 | throw ex; 1359 | } 1360 | } 1361 | 1362 | private string FixedPath(string path) 1363 | { 1364 | path = (path.StartsWith("/")) ? path : "/" + path; 1365 | return (path.EndsWith("/")) ? path.Remove(path.Length - 1) : path; 1366 | } 1367 | 1368 | private string ReturnDataStringForRestRequest(SpList list) 1369 | { 1370 | StringBuilder str = new StringBuilder(); 1371 | foreach (KeyValuePair property in list.Properties) 1372 | { 1373 | if (property.Key == "Title" || 1374 | property.Key == "Description" || 1375 | property.Key == "ContentTypesEnabled" || 1376 | property.Key == "AllowContentTypes" || 1377 | property.Key == "EnableAttachments" || 1378 | property.Key == "EnableFolderCreation" || 1379 | property.Key == "EnableMinorVersions" || 1380 | property.Key == "EnableModeration" || 1381 | property.Key == "EnableVersioning" || 1382 | property.Key == "ForceCheckout" || 1383 | property.Key == "HasExternalDataSource" || 1384 | property.Key == "Hidden" || 1385 | property.Key == "IrmEnabled" || 1386 | property.Key == "IrmExpire" || 1387 | property.Key == "IrmReject" || 1388 | property.Key == "IsApplicationList" || 1389 | property.Key == "IsCatalog" || 1390 | property.Key == "IsPrivate" || 1391 | property.Key == "MultipleDataList" || 1392 | property.Key == "NoCrawl" || 1393 | property.Key == "ServerTemplateCanCreateFolders" || 1394 | property.Key == "BaseTemplate") 1395 | str.Append("'" + property.Key + "':'" + property.Value + "',"); 1396 | } 1397 | str.Length--; 1398 | return str.ToString(); 1399 | } 1400 | 1401 | private string ReturnDataStringForRestRequest(SpItem item) 1402 | { 1403 | StringBuilder str = new StringBuilder(); 1404 | foreach (KeyValuePair data in item.Data) 1405 | { 1406 | if (data.Key != "ID" && data.Key != "Id" && !string.IsNullOrEmpty(data.Value)) 1407 | { 1408 | if (data.Value.StartsWith("{")) 1409 | str.Append("'" + data.Key + "':" + data.Value + ","); 1410 | else 1411 | str.Append("'" + data.Key + "':'" + data.Value + "',"); 1412 | } 1413 | } 1414 | str.Length--; 1415 | return str.ToString(); 1416 | } 1417 | 1418 | private XmlNamespaceManager ReturnSpXmlNameSpaceManager(XmlDocument xmlDoc) 1419 | { 1420 | try 1421 | { 1422 | XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable); 1423 | manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); 1424 | manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); 1425 | manager.AddNamespace("atom", "http://www.w3.org/2005/Atom"); 1426 | return manager; 1427 | } 1428 | catch (Exception ex) 1429 | { 1430 | throw ex; 1431 | } 1432 | } 1433 | #endregion 1434 | } 1435 | } 1436 | --------------------------------------------------------------------------------