1

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

  • 1
    i am not developer but i can see the URL is not correct...could you please check that – Waqas Sarwar MVP Dec 5 '16 at 0:57
  • @WaqasSarwarMCSE now the url inside this "string currentweburl = properties.WebUrl;" is correct absolute url for my sub-site ,, now i try this "properties.RelativeWebUrl" instead of "properties.WebUrl" ,,and seems the error is gone... seems i should submit the relative url?? – john Gu Dec 5 '16 at 1:52
1

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.

  • now i just replace "string currentweburl = properties.WebUrl;" with "string currentweburl = properties.RelativeWebUrl" and the error is gone,, and the sub-site is created successfully .. so should i run it in your way or my appraoch of passing the relative url is fine also ?? – john Gu Dec 5 '16 at 2:04
  • 1
    Hi John, both are fine, and they both get the relative urls for the web. My method is just dependent on the site collection itself and goes to check the web. It's easier for me to know what's going on if I am using it everywhere, so I check for the site collection, then drill down to the heirarchy of the sites to create my new site. Easier to know what's going on, but your way is absolutely fine as well and you can keep it. – Mohamed Derhalli Dec 5 '16 at 2:09
  • Comments are not for extended discussion; this conversation has been moved to chat. – Benny Skogberg Dec 6 '16 at 5:24
1

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

  • as per the documentation i need to pass the relative url for my sub-site ,,, so when i used "properties.RelativeWebUrl" instead of "properties.WebUrl".. the error is gone,, not 100% sure what is going on !! – john Gu Dec 5 '16 at 1:54
  • properties.WebUrl will give you the absolute url something like http://server1/sitecollection/site whereas relative url will only return /sitecollection/site – Gaurav Vijh Dec 5 '16 at 2:00
  • yes i know the differences between relative and absolute urls .. but seems for creating sub-sites i need to pass the relative urls not the absolute url is this correct ? – john Gu Dec 5 '16 at 2:06
  • 1
    yes correct, Also its same for both Site and sub sites. You need to pass the relative url for creating site collection or sub site. – Gaurav Vijh Dec 5 '16 at 2:11
1

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.

  • yes using relative url have fix the problem.. – john Gu Dec 5 '16 at 15:19

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.