Question

I am working on building an event receiver inside my sharepoint server 2013.

now i have the following structure:-

  • site collection url = http://servrname/kb/pmo
  • sub-site url = http://servername/kb/pmo/projects/
  • inside the subsite i have a list named "projects". where i define an event receiver which will fire when the item is added. and the event receiver will create a sub-site under the projects. such as "http://servername/kb/pmo/projects/SubSiteA"

here is the related code inside my event receiver which is scoped at the web level:-

    public override void ItemAdded(SPItemEventProperties properties)
            {
                   base.ItemAdded(properties);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(properties.SiteId))
            {
                string currentweburl = properties.WebUrl;



                    using (SPWeb spCurrentSite = site.OpenWeb(currentweburl))
                    {
                     //code goes here
        SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, "123", Convert.ToUInt16(1033), webTemplate, false, false);
        newSite.Update();
        newSite.Close();

But I am getting this strange error on the last line (when I am trying to create a new SPWeb)

<nativehr>0x80070002</nativehr><nativestack></nativestack>There is no Web named "/kb/pmo/http://servername/kb/pmo/projects".

so can anyone advice on this please?

Thanks

Was it helpful?

Solution

You can try something like this:

 newWeb = site.Webs.Add(string.Concat(web.ServerRelativeUrl, "/",curItemSiteName), curItemSiteName, "", 1033, "CMSPUBLISHING#0", false, false);

Here, the "site" is your current SPSite object, you add a new web to it under the url of your current web where the code is running and append /curItemSiteName.

You can use this as a standard to creation for a web object under any location in your site.

OTHER TIPS

can you try this:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
  using(SPWeb myWeb = mySite.OpenWeb())
  {
    SPweb newWeb = myWeb.Webs.Add("MyNewSitePath","My New Site","My site description",1033,"my site template name",false,false);
  }
}

Also, refer to SPWebCollection.Add

The error you are getting is telling us that the URL is not correct, you have to URL correct way.Just see the URL "/kb/pmo/http://servername/kb/pmo/projects".

Now make sure you are calling the correct Url, I would prefer the relative URLs as my developer always use it. I don't know whether it is best practices or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top