We need to populate some data on an application page. The application page is present in the layouts folder and hence is accessible via the respective site URL. There are multiple site collections. Each site collection has their own set of users (unique) who do not interfere or have rights with other sitecollections.
There is a list in Root Site collection's(the 1st site collection we create after creating a web application) root web.
I need to pull in the data from this list into the application page.
My question is how do I access the root site collections, root web using the SP Server Object model from any other site collections web?
Also, what would be the security implications, because we cannot have a user of say site collection 2 or 3 or 4 to have any rights in the 1st site collection. Does that mean that this scenario is not possible? Is there any other workaround?
The requirement is to have a policy page which should pick up data from a list and show to users. The user wants the policy page data to be dynamically picked from the 1st item of the list. This page is an application page and hence is accessible to each user on every site collection's web. However, my concern is regarding the way to access this data from sitecollection[0].rootweb and regarding security implications as users of other site collections do not have any rights on sitecollection[0].
UPDATE1
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb rootSiteColRootWeb = SPContext.Current.Site.WebApplication.Sites[0].RootWeb;
SPList spList = rootSiteColRootWeb.Lists.TryGetList("MyMenu");
if (spList != null)
{
SPQuery qry = new SPQuery();
qry.Query =
@" <OrderBy>
<FieldRef Name='Title' />
</OrderBy>";
qry.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Data' />";
SPListItemCollection listItems = spList.GetItems(qry);
contentsDiv.InnerHtml = listItems[0]["Data"].ToString();
}
});
Update2
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.WebApplication.Sites[0].RootWeb.Url))
{
SPWeb rootWeb = site.RootWeb;
SPList spList = rootWeb.Lists.TryGetList("MyMenu");
if (spList != null)
{
SPQuery qry = new SPQuery();
qry.Query =
@" <OrderBy>
<FieldRef Name='Title' />
</OrderBy>";
qry.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Data' />";
SPListItemCollection listItems = spList.GetItems(qry);
contentsDiv.InnerHtml = listItems[0]["Data"].ToString();
}
}
});
The code present in both Update 1 and update 2 run successfully when user with no rights on the sitecollection on which the list is present logs in. I want to know which is better method and why?