0

I have List created in the Parent Web and created a Visual WebPart to pull the data from the list and display. When I add the same WebPart in the SubSite pages the data is not populating to display. When I try the following condition:

String siteUrl = SPContext.Current.Web.ParentWeb.Url; //Featch the top site url

data is populating in the SubSites and not display in the Parent site.

How can I conditionally change the site url path for Parent site and Sub sites to refer the list created in Parent site.

  • can you please explain what you mean? one moment you refer to parent has the list and child has webpart and then below code you say your child site gets data but parent doesnt??? so which one is it ;) , when you say conditionally what do you mean by that? do you mean wherever you have the webpart you want it to refer to the parent if its in parent or any child? – Ali Jafer Dec 2 '14 at 9:22
  • List is creted in the Parent site and WebPart is developed and used across the Pareant and Subsites, so it is a single list refering across the Site Collections site. – Umesh Bagalur Dec 2 '14 at 9:38
1

You should get the current site collection rootweb url as below

String siteUrl = SPContext.Current.Site.RootWeb.Url;
0

i think you might be getting confused ;)

SPContext.Current.Web.ParentWeb.Url is fine if you do:

SPContext.Current.Web.RootWeb.Url 

root web is the web at the root level (top most level) of the site collection!

if its at root which i think your refering to??:

SPWeb web = SPContext.Current.Web.RootWeb

//you are now in the root parent spweb object
SPList list = web.Lists["MyCustomList"];
SPListItemCollection itemCol = list.Items;

foreach (SPListItem item in itemCol)
{
    //do what you want with each item
}

the above code would work if you were to put the webpart on any site or subsite as its refering to the root site of all sites and subsites.

if your not happy with the code above than you can use:

String siteUrl = SPContext.Current.Web.Site.Url; //Featch the top site url 

above also gets the root site url for that given sitecollection! eith use it as it is as it will give you the url or:

SPSite site = SPContext.Current.Web.Site; 
using(SPWeb web = site.OpenWeb())
{
   //you are now in the root parent spweb object
   SPList list = web.Lists["MyCustomList"];
   SPListItemCollection itemCol = list.Items;

   foreach (SPListItem item in itemCol)
   {
       //do what you want with each item
   }
}
0

To access the list in Parent site from the subiste we can check the based on the condition of current web url:

String siteUrl = SPContext.Current.Web.Url;  //Fetch the top site url                

//Fetch the top site url if it is any subsite 
if ( siteUrl.ToLower().Contains("/blog") )
{
     siteUrl = SPContext.Current.Web.ParentWeb.Url;
}

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.