Recently, I got an requirement to create a SharePoint sub site from the some .Net application which is hosted somewhere hosted outside the SharePoint environment. It is provided that the .Net application can be added in same domain. So if I brief about requirement here:
- Need to create a sub site in SharePoint site collection based on some default template(later on custom template)
- Use REST(not even CSOM) will be used in order to create.
- API call will be send from some other machine(not SharePoint machine) which is in same domain and can access the SharePoint site.
There are few challenges which I faces if we try to work with such kind of requirements. The authentication tokens and RequestDigest data are the crucial things to retrieve.
I have explored some way to execute REST API and create a sub site without navigating to SharePoint site. Follow the given steps to achieve the same:
1) Create a simple console application with c# selected language.
2) Next is to install the Newtonsoft.Json. Navigate to Visual studio menu: “View” > “Other Windows” > “Package Manager Console” and Then type the following: “Install-Package Newtonsoft.Json”
3) Now install RestSharp using the package manager. Type the following: “Install-Package RestSharp”
4) Now open the Program.cs file of console application and paste the code below:
RestClient RC = new RestClient(“http:/SharepointSiteUrl/_api”);
NetworkCredential NCredential = System.Net.CredentialCache.DefaultNetworkCredentials;//new NetworkCredential(“mohitvashishtha”, “******”, “mydomain”);
RC.Authenticator = new NtlmAuthenticator(NCredential);
Console.WriteLine(“Creating Rest Request”);
RestRequest Request = new RestRequest(“contextinfo?$select=FormDigestValue”, Method.POST);
string ReturnedStr = RC.Execute(Request).Content;
int StartPos = ReturnedStr.IndexOf(“FormDigestValue”) + 18;
int length = ReturnedStr.IndexOf(@”””,”, StartPos) – StartPos;
string FormDigestValue = ReturnedStr.Substring(StartPos, length);
var Data = string.Concat(
“‘Title’:’Team projects 3′,’Url’:’TeamProjects3′,’WebTemplate’:’STS’,”,
Request = new RestRequest(“web/webs/add”, Method.POST);
Request.RequestFormat = DataFormat.Json;
Request.AddParameter(“application/json;odata=verbose”, Data, ParameterType.RequestBody);
Console.WriteLine(“SharePoint Site is successfully created.”);
5) Now application is ready to run. Make sure that all the namespace issues are resolved.
6) It will take some time to create site but output window will display the details it got from the Request.
In case of already existing site:
Just a little explanation about the above code, this code is actually getting the request digest first and then in second REST request it will pass the same reader digest. There are few chances that this code will throw 401 unauthorized error if user does not have permission on sharepoint site. But there are option to hardcode the credential too using NetworkCredentials object commented in above code.
Hope this information is helpful to you. Thanks for time.
Happy SharePointing 🙂