3

I have created a provider hosted app with adfs. In this app I have the SP clientContext of the site collection. I have a form in this app. When the user submits the form, there will be created a subsite.

// Set the clientcontext for the SPHost
Microsoft.SharePoint.Client.ClientContext clientContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current).CreateUserClientContextForSPHost();

// Create new subsite
Web newWeb = clientContext.Web.CreateWeb(new OfficeDevPnP.Core.Entities.SiteEntity()
            {
                Title = "subsite1",
                Url = "subsite1",
                Description = "subsite1",
                Template = "BLANKINTERNET#0",
                Lcid = (uint)1043
            }, true, true);

After creating "subsite1" I would like to create a second subsite under "subsite1". But I need first to get the client context of "subsite1".

How can I get the client context of an spweb? Maybe by url?

UPDATE

I am trying now to open the subsite1 and create a subsite2 under subsite1. I got the exception:

Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap.

And this is the stacktrace:

at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay)
   at Microsoft.SharePoint.Client.WebExtensions.CreateWeb(Web parentWeb, String title, String leafUrl, String description, String template, Int32 language, Boolean inheritPermissions, Boolean inheritNavigation)
   at Microsoft.SharePoint.Client.WebExtensions.CreateWeb(Web parentWeb, SiteEntity subsite, Boolean inheritPermissions, Boolean inheritNavigation)
   at CompanyName.SharePoint.ProvisioningWeb.Controllers.HomeController.Index() in E:\VSO\CompanyName\Main\CompanyName\CompanyName.SharePoint.ProvisioningWeb\Controllers\HomeController.cs:line 38
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

Here is the code:

[SharePointContextFilter]
        public ActionResult Index()
        {
            User spUser = null;

            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    spUser = clientContext.Web.CurrentUser;

                    clientContext.Load(spUser, user => user.Title);

                    clientContext.ExecuteQuery();

                    ViewBag.UserName = spUser.Title;

                    /////////////////////

                    var subweb1 = clientContext.Site.OpenWeb("sub1");
                    clientContext.Load(subweb1);
                    clientContext.ExecuteQueryRetry();

                    // Create new subsite
                    Web sub2 = subweb1.CreateWeb(new OfficeDevPnP.Core.Entities.SiteEntity()
                    {
                        Title = "sub2",
                        Url = "sub2",
                        Description = "sub2", // todo, deze beschrijving wordt niet opgeslagen in de site
                        Template = "BLANKINTERNET#0",
                        Lcid = (uint)1043
                    }, true, true);

                    // Load the url property (office dev extension method)
                    sub2.EnsureProperty(w => w.Url);

                    string sub2Title = sub2.Title;
                }
            }

            return View();
        }
  • If your issue is resolved, please don't forget to mark the answer as accepted and upvote if you find the answer helpful – Sergei Sergeev Nov 17 '16 at 18:11
2

The easiest way is to construct web object using OpenWeb method like this:

var subweb = clientContext.Site.OpenWeb("subsite1");
clientContext.Load(subweb);
clientContext.ExecuteQueryRetry();  

Later you can you use subweb in order to create a new site.

  • I try this solution. But when I try to create subsite2 I got this exception: Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap. – Ola Nov 17 '16 at 15:24
  • Are you using server side object model? – Sergei Sergeev Nov 17 '16 at 15:26
  • no, this is an provider hosted app. Only CSOM and pnp – Ola Nov 17 '16 at 15:27
  • Can you please post stack trace of the exception? – Sergei Sergeev Nov 17 '16 at 15:27
  • See update for the stacktrace – Ola Nov 17 '16 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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