Suppose I have two buttons: one that created a subsite and the other that gets the id of the newly created subsite and inserts it into a webpart's properties.

Qustion 1: How can I get the ID of the newly created subsite programmatically?

Qustion 2: How can I insert the ID of the new subsite into a webpart that exists already?

Any code or reference is much appreciated.


I think you can try using an event receiver.

Suppose say, you want to something done on the creation of a site being provisioned. You can add an event receiver item and select something like A web was provisioned.

And in your code this can go in for getting the web ID -

public override void WebProvisioned(SPWebEventProperties properties)
        using (SPSite site = new SPSite("your url goes in here"))
            SPWeb web = properties.Web; // this will get you the properties for the newly created site
            //using (SPWeb w = web)
                Guid webID = w.ID;

Hope this gives you some sort of reference and help you in reaching somewhere with part 2 of your query too.

|improve this answer|||||
  • 1
    Hi Sane.Amit, your answer points me in the right direction. Now I have to handle part two. – Imir Hoxha Apr 23 '14 at 12:43

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.