PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ItemConsole/UpdateExample.cs

https://github.com/herskinduk/Usergroup.ItemWebApi
C# | 41 lines | 31 code | 5 blank | 5 comment | 0 complexity | 6b566c81512ad426a39e028f6e48231e MD5 | raw file
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace ItemConsole
  12. {
  13. public class UpdateExample
  14. {
  15. public static void UpdateFieldOnChildrenOfHome(string fieldName, string fieldValue)
  16. {
  17. // Setup Item request
  18. var request = WebRequest.CreateHttp("http://usergroup/-/item/v1/sitecore/content/home?scope=c");
  19. // Note: The scope is set to children -> ^^^^^^^
  20. // Set username and password headers
  21. request.Headers.Add("X-Scitemwebapi-Username", "admin");
  22. request.Headers.Add("X-Scitemwebapi-Password", "b");
  23. request.Method = "PUT";
  24. // Create a query string formatted key value list with fields to update
  25. var data = Encoding.UTF8.GetBytes(string.Format("{0}={1}", HttpUtility.UrlEncode(fieldName), HttpUtility.UrlEncode(fieldValue)));
  26. request.ContentLength = data.Length;
  27. request.ContentType = "application/x-www-form-urlencoded";
  28. // Write post data to request stream
  29. var requestStream = request.GetRequestStream();
  30. requestStream.Write(data, 0, data.Length);
  31. requestStream.Close();
  32. var response = (HttpWebResponse)request.GetResponse();
  33. ConsoleExt.WriteJsonStream(response.GetResponseStream());
  34. }
  35. }
  36. }