1

I have an SP site collection.

My requirement is to create a sub site in this site collection from an MVC application. When I enter the name of the site and click on 'Create' button from MVC site, a sub site with entered name should be created in site collection.

5
  • Where your MVC application is hosted? May 12, 2016 at 4:53
  • SharePoint version ?
    – Gaurravs
    May 12, 2016 at 4:56
  • @ Aakash Morya: application is hosted in IIS.
    – Anjitha
    May 12, 2016 at 5:09
  • Your SharePoint and MVC are hosted on the same server? May 12, 2016 at 5:13
  • no both are on different servers.
    – Anjitha
    May 12, 2016 at 5:16

1 Answer 1

2

You can do that using Client Object Model C#. Add .dlls from NuGet in your MVC project.

Install-Package Microsoft.SharePoint.Client.dll

Here is an example. Don't forget to pass a credential.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.Utilities;

    private void CreatenewSite()
            {
                string SiteDescription = “This is my first site creation using Client Object Model.”;
                int SiteLanguage = 1033;
                string SiteTitle = “My first SharePoint School Site”;
                string SiteUrl = “miniSharePointSchool”;
                bool SitePermissions = false;
                string mywebTemplate = “STS#0”;

                ClientContext clientContext = new ClientContext(“http://SharePointSchool/“);

                clientContext.Credentials = new NetworkCredential("user", "password", "domain");

                //Retreive the web from the Client Context. This web is the root web by default.
                Web oWebsite = clientContext.Web;

                //Create a new webCreateInformation object to specify the properties of the new site being created.

                WebCreationInformation webCreateInfo = new WebCreationInformation();
                webCreateInfo.Description = SiteDescription;
                webCreateInfo.Language=SiteLanguage;
                webCreateInfo.Title= SiteTitle;
                webCreateInfo.Url=SiteUrl;
                webCreateInfo.UseSamePermissionsAsParentSite= SitePermissions;
                webCreateInfo.WebTemplate=mywebTemplate;

                //Adding a new site under the root web

                Web oNewWebsite = oWebsite.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(oNewWebsite,
                    Website => Website.ServerRelativeUrl,
                    Website => Website.Created);

                // Executes the query to retreive the required information from the server.
                clientContext.ExecuteQuery();
                Console.WriteLine(“Site Created on: ” + oNewWebsite.Created);
                Console.WriteLine(“with Site Title: ” + oNewWebsite.Title);
                Console.ReadLine();
            }
7
  • This code will work if this application and SharePoint Application are hosted on same server. But the user has specified his MVC application is hosted on different server. May 12, 2016 at 5:32
  • Nope! It must work from different server. Just the user will have to pass a credential. CSOM is the one & only way to talk with SharePoint from outside May 12, 2016 at 5:35
  • Yes @Atish, I was worried about the authentication part only :-) May 12, 2016 at 5:43
  • @ Atish: I got an error 'The type or namespace name 'NetworkCredential' could not be found '.I googled and added the Runtime.dll. But it throws an error Runtime.dll namespace can't be found. I have added the refernce for Runtime.dll.
    – Anjitha
    May 12, 2016 at 7:01
  • Add System.Net dll in reference May 12, 2016 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.