Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string contextUrl = System.Configuration.ConfigurationManager.AppSettings["SiteUrl"];
- [Route("SaveProject")]
- [HttpPost]
- public IHttpActionResult SaveProject(SaveProjectViewModel projectViewModel)
- {
- string sharepointTemplate = "{025A81C1-D660-4191-9E04-28FE88399CCC}#Support Project Template";
- bool isActivepro = true;
- string title = "JJ";
- string description = "JJ DESC";
- string templateId = sharepointTemplate;
- bool isProjectActive = isActivepro;
- string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
- string password = System.Configuration.ConfigurationManager.AppSettings["PassWord"];
- var securePassword = new SecureString();
- foreach (char c in password.ToCharArray())
- {
- securePassword.AppendChar(c);
- }
- using (ClientContext clientContext = new ClientContext(contextUrl))
- {
- clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
- clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
- Microsoft.SharePoint.Client.Web oWebsite = clientContext.Web;
- clientContext.Load(oWebsite);
- //clientContext.ExecuteQuery();
- //CreateNewSite(clientContext, title, description, templateId);
- int siteLanguage = 1033;
- // clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
- Microsoft.SharePoint.Client.Web webSite = clientContext.Web;
- clientContext.Load(webSite, wde => webSite.Webs);
- clientContext.ExecuteQuery();
- var subWeb = (from w in webSite.Webs where w.Title == title select w).SingleOrDefault();
- if (subWeb == null)
- {
- //Create a new webCreateInformation object to specify the properties of the new site being created.
- WebCreationInformation webCreateInfo = new WebCreationInformation();
- webCreateInfo.Description = description;
- webCreateInfo.Language = siteLanguage;
- webCreateInfo.Title = title;
- webCreateInfo.Url = title;
- webCreateInfo.UseSamePermissionsAsParentSite = true;
- webCreateInfo.WebTemplate = templateId;
- //Adding a new site under the root web
- Microsoft.SharePoint.Client.Web newWebsite = webSite.Webs.Add(webCreateInfo);
- // Retreiving the server properties to the client context. Here we are retreiving the web server url and the time it was created.
- clientContext.Load(newWebsite,
- Website => Website.ServerRelativeUrl,
- Website => Website.Created);
- clientContext.ExecuteQuery();
- }
- else
- {
- // site already exist.
- }
- //Save Project To List
- List list = clientContext.Web.Lists.GetByTitle("Project List");
- clientContext.Load(list);
- var getSiteName = new CamlQuery();
- getSiteName.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + title + "</Value></Eq></Where></Query></View>";
- ListItemCollection listItems = list.GetItems(getSiteName);
- clientContext.Load(list);
- clientContext.Load(listItems);
- clientContext.ExecuteQuery();
- if (listItems.Count == 0)
- {
- ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
- ListItem listItem = list.AddItem(itemCreateInfo);
- FieldUrlValue hyper = new FieldUrlValue();
- hyper.Description = title;
- hyper.Url = contextUrl + title;
- listItem["Title"] = title;
- listItem["Project_x0020_Name"] = hyper;
- listItem["IsActive"] = title;
- listItem.Update();
- // Executes the query to retreive the required information from the server.
- clientContext.ExecuteQuery();
- }
- else
- {
- //item already exist in list.
- }
- }
- return Ok(new
- {
- NewId = '1',
- Message = "Sucessfully saved project.",
- IsSuccess = true
- });
- }
- <input type="button" value="Save" onclick="OnSaveClick()">
- <script>
- function OnSaveClick() {
- $.ajax({
- type: "post",
- url: 'http://localhost:myhost/api/Project/SaveProject',
- data: {
- //'Id': '101',
- //'Description': 'this is a test project',
- //'Name': 'VSO Test Project 1',
- //'IsActive': '1',
- //'SharepointTemplateID': 4,
- },
- }).done(function (data) {
- debugger;
- if (data.IsSuccess) {
- } else {
- }
- }).fail(function (jqXHR, textStatus) {
- toastr["error"](jqXHR + textStatus);
- });
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement