Advertisement
Guest User

Untitled

a guest
May 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. public string contextUrl = System.Configuration.ConfigurationManager.AppSettings["SiteUrl"];
  2.  
  3. [Route("SaveProject")]
  4. [HttpPost]
  5. public IHttpActionResult SaveProject(SaveProjectViewModel projectViewModel)
  6. {
  7.  
  8. string sharepointTemplate = "{025A81C1-D660-4191-9E04-28FE88399CCC}#Support Project Template";
  9. bool isActivepro = true;
  10. string title = "JJ";
  11. string description = "JJ DESC";
  12. string templateId = sharepointTemplate;
  13. bool isProjectActive = isActivepro;
  14.  
  15. string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
  16. string password = System.Configuration.ConfigurationManager.AppSettings["PassWord"];
  17. var securePassword = new SecureString();
  18. foreach (char c in password.ToCharArray())
  19. {
  20. securePassword.AppendChar(c);
  21. }
  22. using (ClientContext clientContext = new ClientContext(contextUrl))
  23. {
  24. clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
  25. clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
  26. Microsoft.SharePoint.Client.Web oWebsite = clientContext.Web;
  27. clientContext.Load(oWebsite);
  28. //clientContext.ExecuteQuery();
  29.  
  30. //CreateNewSite(clientContext, title, description, templateId);
  31.  
  32. int siteLanguage = 1033;
  33. // clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
  34.  
  35. Microsoft.SharePoint.Client.Web webSite = clientContext.Web;
  36. clientContext.Load(webSite, wde => webSite.Webs);
  37. clientContext.ExecuteQuery();
  38.  
  39. var subWeb = (from w in webSite.Webs where w.Title == title select w).SingleOrDefault();
  40.  
  41. if (subWeb == null)
  42. {
  43. //Create a new webCreateInformation object to specify the properties of the new site being created.
  44.  
  45. WebCreationInformation webCreateInfo = new WebCreationInformation();
  46. webCreateInfo.Description = description;
  47. webCreateInfo.Language = siteLanguage;
  48. webCreateInfo.Title = title;
  49. webCreateInfo.Url = title;
  50. webCreateInfo.UseSamePermissionsAsParentSite = true;
  51. webCreateInfo.WebTemplate = templateId;
  52.  
  53. //Adding a new site under the root web
  54.  
  55. Microsoft.SharePoint.Client.Web newWebsite = webSite.Webs.Add(webCreateInfo);
  56.  
  57. // Retreiving the server properties to the client context. Here we are retreiving the web server url and the time it was created.
  58.  
  59. clientContext.Load(newWebsite,
  60. Website => Website.ServerRelativeUrl,
  61. Website => Website.Created);
  62. clientContext.ExecuteQuery();
  63. }
  64. else
  65. {
  66. // site already exist.
  67. }
  68.  
  69. //Save Project To List
  70.  
  71. List list = clientContext.Web.Lists.GetByTitle("Project List");
  72. clientContext.Load(list);
  73. var getSiteName = new CamlQuery();
  74. getSiteName.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + title + "</Value></Eq></Where></Query></View>";
  75. ListItemCollection listItems = list.GetItems(getSiteName);
  76. clientContext.Load(list);
  77. clientContext.Load(listItems);
  78. clientContext.ExecuteQuery();
  79.  
  80. if (listItems.Count == 0)
  81. {
  82. ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
  83. ListItem listItem = list.AddItem(itemCreateInfo);
  84. FieldUrlValue hyper = new FieldUrlValue();
  85. hyper.Description = title;
  86. hyper.Url = contextUrl + title;
  87. listItem["Title"] = title;
  88. listItem["Project_x0020_Name"] = hyper;
  89. listItem["IsActive"] = title;
  90. listItem.Update();
  91. // Executes the query to retreive the required information from the server.
  92. clientContext.ExecuteQuery();
  93. }
  94. else
  95. {
  96. //item already exist in list.
  97. }
  98.  
  99. }
  100.  
  101. return Ok(new
  102. {
  103. NewId = '1',
  104. Message = "Sucessfully saved project.",
  105. IsSuccess = true
  106. });
  107.  
  108. }
  109.  
  110. <input type="button" value="Save" onclick="OnSaveClick()">
  111. <script>
  112. function OnSaveClick() {
  113.  
  114. $.ajax({
  115. type: "post",
  116. url: 'http://localhost:myhost/api/Project/SaveProject',
  117. data: {
  118. //'Id': '101',
  119. //'Description': 'this is a test project',
  120. //'Name': 'VSO Test Project 1',
  121. //'IsActive': '1',
  122.  
  123. //'SharepointTemplateID': 4,
  124.  
  125. },
  126. }).done(function (data) {
  127. debugger;
  128. if (data.IsSuccess) {
  129. } else {
  130.  
  131. }
  132.  
  133. }).fail(function (jqXHR, textStatus) {
  134. toastr["error"](jqXHR + textStatus);
  135. });
  136. }
  137.  
  138. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement