.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Parent Site Lookup using GUID in SharePoint

Posted By:Jean Paul       Posted Date: November 30, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we will explore Parent Site Lookup using GUID in SharePoint.


We have a common parent site from where we create new project list items.  On approving each project item, a new project site will be created through automation.  Each project created will be having a SiteGuid column associated, which the site can lookup with the common parent site.



Once a new project item is created & approved, a new project site will be created.  The project site GUID will be stored in the SiteGuid column of Projects list item.


Action 1: Create Projects List

In the common parent site, create a new Projects list with following properties.


The SiteGuid column will be updated by the Automatic site creation method.


Action 2: Create Project list item


Add a new project list item with the following properties.

Please note that, this list item will be accessed from the project sub site.


Action 3: Create Project site

Now we can create our Project sub-site.  Please note that we need to automate the project site creation.  Thus, we are using a console application to create the project site.

Following is the code for the same. (please change urls)



namespace CreateProjectSite
    class Program
        static void Main(string[] args)
            using (SPSite site = new SPSite("http://hp"))
                SPWeb web = site.AllWebs.Add("project1", "Project 1", string.Empty, 0, SPWebTemplate.WebTemplateSTS, false, false);

                SPListItem item = site.AllWebs[0].Lists["Projects"].Items[0]; // Hardcoding
                item["SiteGuid"] = web.ID.ToString();



After running the code, we can see that a new project site is created.


Additionally, the project list item will get updated with the site id as shown below.


Action 4: Create web part

Open Visual Studio, create a new web part project & create UI like below.


Add the following code to it.


protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
        string siteGuid = SPContext.Current.Web.ID.ToString();
        using (SPSite site = new SPSite("http://hp"))
            SPWeb web = site.OpenWeb("/");
            SPList list = web.Lists["Projects"];

            SPQuery oQuery = new SPQuery();
            oQuery.Query = "<Where><Eq><FieldRef Name='SiteGuid'/>" +
                "<Value Type='Text'>" + siteGuid + "</Value></Eq></Where>";
            SPListItemCollection items = list.GetItems(oQuery);

            if (items.Count > 0)
                SPListItem item = items[0];
                IdLabel.Text = siteGuid;
                TitleLabel.Text = item.Title;
                DescriptionLabel.Text = item["Description"].ToString();
                CustomerLabel.Text = item["Customer"].ToString();
                DateLabel.Text = item["Start Date"].ToString() + " " + item["End Date"].ToString();

                foreach (SPFieldUserValue user in (IEnumerable)item["Resources"])
                    ResourcesLabel.Text += user.User.Name + "; ";

                IdLabel.Text = "Unable to find a match!";


Please note that the web part is looking up the parent site & fetching project information from it.


Action 5: Verify Project site

Now run the code & open the project site. You should be able to see the web part is fetching values from the common parent site.


This concludes our idea on keeping project information in the parent site & fetching information from child project site.  I have also tried the property bag option, but it is not persisting application pool recycle.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend