Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.SharePoint.Client;
  8.  
  9.  
  10. namespace RecordCentreRule
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. var targetSite = new Uri("https://YourSite.sharepoint.com");
  17. var login = "SomeOne@SharePoint.onmicrosoft.com";
  18. var password = "YourPassword";
  19. var securePassword = new SecureString();
  20. foreach (char c in password)
  21. {
  22. securePassword.AppendChar(c);
  23. }
  24.  
  25. var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
  26.  
  27. using (ClientContext clientCtx = new ClientContext(targetSite))
  28. {
  29. clientCtx.Credentials = onlineCredentials;
  30. Web web = clientCtx.Web;
  31. List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
  32. ListItem itm = routingRulesList.GetItemById(1);
  33. clientCtx.Load(web);
  34. clientCtx.Load(routingRulesList);
  35. clientCtx.Load(itm);
  36. clientCtx.ExecuteQuery();
  37.  
  38.  
  39. Console.WriteLine("Title: " + itm["Title"] + "n");
  40. Console.WriteLine("RoutingConditions: " + itm["RoutingConditions"] + "n");
  41. Console.WriteLine("RoutingConditionProperties: " + itm["RoutingConditionProperties"] + "n");
  42. Console.WriteLine("RoutingContentType: " + itm["RoutingContentType"] + "n");
  43. Console.WriteLine("RoutingContentTypeInternal: " + itm["RoutingContentTypeInternal"] + "n");
  44. Console.WriteLine("RoutingConditions: " + itm["RoutingConditions"] + "n");
  45. Console.WriteLine("RoutingConditionProperties: " + itm["RoutingConditionProperties"] + "n");
  46. Console.WriteLine("RoutingAliases: " + itm["RoutingAliases"] + "n");
  47. Console.WriteLine("RoutingTargetLibrary: " + itm["RoutingTargetLibrary"] + "n");
  48. Console.WriteLine("RoutingTargetFolder: " + itm["RoutingTargetFolder"] + "n");
  49. Console.WriteLine("RoutingTargetPath: " + itm["RoutingTargetPath"] + "n");
  50. Console.WriteLine("RoutingAutoFolderProp: " + itm["RoutingAutoFolderProp"] + "n");
  51. Console.WriteLine("RoutingAutoFolderSettings: " + itm["RoutingAutoFolderSettings"] + "n");
  52. Console.WriteLine("RoutingCustomRouter: " + itm["RoutingCustomRouter"] + "n");
  53. Console.WriteLine("RoutingRuleExternal: " + itm["RoutingRuleExternal"] + "n");
  54. Console.Read();
  55.  
  56.  
  57. }
  58.  
  59. }
  60. }
  61. }
  62.  
  63. # replace these details (also consider using Get-Credential to enter password securely as script runs)..
  64. $username = "SomeOne@SharePoint.onmicrosoft.com"
  65. $password = "YourPassword"
  66. $url = "https://YourSite.sharepoint.com"
  67.  
  68. $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
  69.  
  70. # the path here may need to change if you used e.g. C:Lib..
  71. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  72. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  73.  
  74.  
  75. # connect/authenticate to SharePoint Online and get ClientContext object..
  76. $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
  77. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
  78. $clientContext.Credentials = $credentials
  79.  
  80. if (!$clientContext.ServerObjectIsNull.Value)
  81. {
  82. Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green
  83.  
  84. $web = $clientContext.Site.RootWeb
  85. $listRoutingRules = $web.Lists.GetByTitle("Content Organizer Rules")
  86. $item = $listRoutingRules.GetItemById(1)
  87. $clientContext.Load($listRoutingRules)
  88. $clientContext.Load($item)
  89. $clientContext.ExecuteQuery()
  90. Write-Host "Title: " $item["Title"]
  91. Write-Host "RoutingConditions: " $item["RoutingConditions"]
  92. Write-Host "RoutingConditionProperties: " $item["RoutingConditionProperties"]
  93. Write-Host "RoutingContentType: " $item["RoutingContentType"]
  94. Write-Host "RoutingContentTypeInternal: " $item["RoutingContentTypeInternal"]
  95. Write-Host "RoutingConditions: " $item["RoutingConditions"]
  96. Write-Host "RoutingConditionProperties: " $item["RoutingConditionProperties"]
  97. Write-Host "RoutingAliases: " $item["RoutingAliases"]
  98. Write-Host "RoutingTargetLibrary: " $item["RoutingTargetLibrary"]
  99. Write-Host "RoutingTargetFolder: " $item["RoutingTargetFolder"]
  100. Write-Host "RoutingTargetPath: " $item["RoutingTargetPath"]
  101. Write-Host "RoutingAutoFolderProp: " $item["RoutingAutoFolderProp"]
  102. Write-Host "RoutingAutoFolderSettings: " $item["RoutingAutoFolderSettings"]
  103. Write-Host "RoutingCustomRouter: " $item["RoutingCustomRouter"]
  104. Write-Host "RoutingRuleExternal: " $item["RoutingRuleExternal"]
  105.  
  106. }
  107.  
  108. using System;
  109. using System.Collections.Generic;
  110. using System.Linq;
  111. using System.Security;
  112. using System.Text;
  113. using System.Threading.Tasks;
  114. using Microsoft.SharePoint.Client;
  115.  
  116.  
  117. namespace RecordCentreRule
  118. {
  119. class Program
  120. {
  121. static void Main(string[] args)
  122. {
  123. var targetSite = new Uri("https://YourSite.sharepoint.com");
  124. var login = "SomeOne@SharePoint.onmicrosoft.com";
  125. var password = "YourPassword";
  126. var securePassword = new SecureString();
  127. foreach (char c in password)
  128. {
  129. securePassword.AppendChar(c);
  130. }
  131.  
  132. var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
  133.  
  134. using (ClientContext clientCtx = new ClientContext(targetSite))
  135. {
  136. clientCtx.Credentials = onlineCredentials;
  137. Web web = clientCtx.Web;
  138. List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
  139. clientCtx.Load(routingRulesList);
  140. clientCtx.ExecuteQuery();
  141.  
  142.  
  143. ListItemCreationInformation routingRuleInfo = new ListItemCreationInformation();
  144. ListItem routingRule = routingRulesList.AddItem(routingRuleInfo);
  145. routingRule["Title"] = "From Console";
  146. routingRule["RoutingRuleName"] = "From Console";
  147. routingRule["RoutingRuleDescription"] = "From Console";
  148. routingRule["RoutingPriority"] = 1;
  149. routingRule["RoutingEnabled"] = true;
  150. routingRule["RoutingContentType"] = "Your Content Type Name";
  151. routingRule["RoutingContentTypeInternal"] = "0x000000000000000000000000000000000000000000000000000000|Your Content Type Name";
  152. routingRule["RoutingConditions"] = "<Conditions><Condition Column="xxxx-xxx-xxx-xxx-xxxxx|Column|Column Name" Operator="EqualsOrIsAChildOf" Value="1;#WhatEver|xxxx-xxx-xxx-xxx-xxxx" /></Conditions>";
  153. routingRule["RoutingConditionProperties"] = "Column Name on which you need condition";
  154. routingRule["RoutingAliases"] = "Your Content Type Name";
  155. routingRule["RoutingTargetLibrary"] = "Target Library";
  156. routingRule["RoutingTargetFolder"] = "";
  157. routingRule["RoutingTargetPath"] = "/sites/YourSite/Target Library";
  158. routingRule["RoutingAutoFolderProp"] = "Folder Property";
  159. routingRule["RoutingAutoFolderSettings"] = "<AutoFolder><Properties><Property Name="AutoFolderEnabled" Value="True" /><Property Name="AutoFolderPropertyName" Value="Folder Property" /><Property Name="AutoFolderPropertyInternalName" Value="WhatEver" /><Property Name="AutoFolderPropertyID" Value="xxxx-xxxx-xxx-xxx-xxxx" /><Property Name="AutoFolderPropertyFormat" Value="%1 - %2" /><Property Name="AutoFolderPropertyTypeAsString" Value="TaxonomyFieldType" /><Property Name="AutoFolderPropertyTermStore" Value="xxxx-xxx-xxx-xxx-xxxxx" /></Properties></AutoFolder>";
  160. routingRule["RoutingCustomRouter"] = "";
  161. routingRule["RoutingRuleExternal"] = false;
  162.  
  163. routingRule.Update();
  164. clientCtx.ExecuteQuery();
  165. Console.WriteLine("Rule created successfully");
  166. Console.Read();
  167.  
  168. }
  169.  
  170. }
  171. }
  172. }
  173.  
  174. # replace these details (also consider using Get-Credential to enter password securely as script runs)..
  175. $username = "SomeOne@SharePoint.onmicrosoft.com"
  176. $password = "YourPassword"
  177. $url = "https://YourSite.sharepoint.com"
  178.  
  179. $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
  180.  
  181. # the path here may need to change if you used e.g. C:Lib..
  182. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  183. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  184.  
  185.  
  186. # connect/authenticate to SharePoint Online and get ClientContext object..
  187. $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
  188. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
  189. $clientContext.Credentials = $credentials
  190.  
  191. if (!$clientContext.ServerObjectIsNull.Value)
  192. {
  193. Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green
  194.  
  195. $web = $clientContext.Site.RootWeb
  196. $listRoutingRules = $web.Lists.GetByTitle("Content Organizer Rules")#RoutingRules
  197. $clientContext.Load($listRoutingRules)
  198. $clientContext.ExecuteQuery()
  199.  
  200. #Add an item to the list
  201. $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
  202. $Item1 = $listRoutingRules.AddItem($ListItemInfo)
  203. $Item1["Title"] = "Asad Test Rule"
  204. $Item1["RoutingRuleName"] = "Asad Test Rule"
  205. $Item1["RoutingRuleDescription"] = "Asad Test Rule"
  206. $Item1["RoutingPriority"] = 1
  207. $Item1["RoutingEnabled"] = $true
  208. $Item1["RoutingContentType"] = "Your Content Type Name"
  209. $Item1["RoutingContentTypeInternal"] = "0x000000000000000000000000000000000000000000000000000000|Your Content Type Name"
  210. $Item1["RoutingConditions"] = '<Conditions><Condition Column="xxxx-xxx-xxx-xxx-xxxxxx|ColumnName|Column Name" Operator="EqualsOrIsAChildOf" Value="1;#Value|xxxx-xxx-xxx-xxx-xxxxxx" /></Conditions>'
  211. $Item1["RoutingConditionProperties"] = "Condition Property"
  212. $Item1["RoutingAliases"] = "Alias Name"
  213. $Item1["RoutingTargetLibrary"] = "Target Library"
  214. $Item1["RoutingTargetFolder"] = ""
  215. $Item1["RoutingTargetPath"] = "/sites/YourSite/Target Library"
  216. $Item1["RoutingAutoFolderProp"] = "Folder Property"
  217. $Item1["RoutingAutoFolderSettings"] = '<AutoFolder><Properties><Property Name="AutoFolderEnabled" Value="True" /><Property Name="AutoFolderPropertyName" Value="Your Value" /><Property Name="AutoFolderPropertyInternalName" Value="YourValue" /><Property Name="AutoFolderPropertyID" Value="xxxx-xxx-xxx-xx-xxxxx" /><Property Name="AutoFolderPropertyFormat" Value="%1 - %2" /><Property Name="AutoFolderPropertyTypeAsString" Value="TaxonomyFieldType" /><Property Name="AutoFolderPropertyTermStore" Value="xxxx-xxx-xxx-xxx-xxxxxx" /></Properties></AutoFolder>'
  218. $Item1["RoutingCustomRouter"] = ""
  219. $Item1["RoutingRuleExternal"] = $false
  220. $Item1.Update()
  221. $clientContext.ExecuteQuery()
  222. Write-Host "Rule created successfully" -ForegroundColor Green
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement