Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create project site using rest API in SharePoint

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.03k
    Comment on it

    Every project have its own SharePoint sites in which each project has its own documents, tasks,project details,Deliverables, issues and so on.
    For creating that sites first we have to set permissions from settings.First go to PWA Settings > Connected SharePoint Sites > Settings.
    In Settings tab set permission "Allow user to choose".
    Then we are able to create from browser as well as programtically.

    Here below is the sample code for creating project site using rest API.

     using System.Net;
    	    CookieContainer cookie;
         	private CookieContainer Cookies
            {
                get
                {
                    if (cookie == null)
                        cookie = GetAuthCookies();
                    return cookie;
                }
                set
                {
                    cookie = value;
                }
            }
    	    private DateTime _expires = DateTime.Now;
            private string _formDigest;
            private string FormDigest
            {
                get
                {
                    if (_formDigest == null)
                    {
                        return GetFormDigest();
                    }
                    return _formDigest;
                }
            }
    		 /// <summary>
            /// Create project site
            /// </summary>
    		/// <param name="projectId"> pass the project id for which you want to create project site url</param>
    	   private void CreateProjectSite(Guid projectId)  
            {
                string responseText = string.Empty;
                try
                {
                    var endPoint = string.Format("/_api/ProjectServer/Projects(guid'{0}')/CreateProjectSite('')", projectId);
                    var request = (HttpWebRequest)WebRequest.Create("https://testprojectonline.sharepoint.com/sites/pwa" + endPoint); 
                    request.CookieContainer = Cookies;
                    request.Headers.Add("X-RequestDigest", FormDigest);
                    request.Method = "POST";
                    request.ContentLength = 0;
                    using (WebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        responseText = ((HttpWebResponse)response).StatusDescription;
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            var result = reader.ReadToEnd();
    
                            // parse the ContextInfo response
                            var resultXml = XDocument.Parse(result);
                        }
                    }
                }
                catch (Exception ex)
                {
                   //catch exception
                }  
            }
    		// <summary>
            /// Retrieve Authentication Cookies 
            /// </summary>
    		 private CookieContainer GetAuthCookies()
            {
                var securePassword = new SecureString();
                foreach (var c in password) { securePassword.AppendChar(c); }
                var credentials = new SharePointOnlineCredentials(userName, securePassword); //pass pwa username and password
                var authCookie = credentials.GetAuthenticationCookie(webUri); //pwa url
                var cookieContainer = new CookieContainer();
                cookieContainer.SetCookies(webUri, authCookie);
                return cookieContainer;
            }
    		private string GetFormDigest()
            {
                    var endpoint = "/_api/contextinfo";
                    var request = (HttpWebRequest)WebRequest.Create("https://testprojectonline.sharepoint.com/sites/pwa" + endpoint);
                    request.CookieContainer = Cookies;
                    request.Method = "POST";
                    request.ContentLength = 0;
    
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            var result = reader.ReadToEnd();
    
                            // parse the ContextInfo response
                            var resultXml = XDocument.Parse(result);
    
                            // get the form digest value
                            var e = from e1 in resultXml.Descendants()
                                    where e1.Name == XName.Get("FormDigestValue", "http://schemas.microsoft.com/ado/2007/08/dataservices")
                                    select e1;
                            _formDigest = e.First().Value;
    
                        }
                    }
                return _formDigest;
            }
    

    We can also create project site in CSOM using CreateProjectSite() method.


     Here below is the sample code  for creating project site using CSOM.

      foreach (PublishedProject pubProject in projectContext.Projects)
        {
                    pubProject.CreateProjectSite(pubProject.Name);
                    projectContext.ExecuteQuery();
        }

        I hope this code will help you.
        
        Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: