D. Sean Kelly

Hi All,

I've search through the forums and I haven't found anything useful for the problem I'm experiencing.

Short question:

- Is it possible to access a list stored in a site from a webpart in a different site

Site #1: http://address/sites/admin (contains the list)

Site #2: http:/address/sites/contentSite (needs to access the list)

I've got the following structure setup:

Portal

- Admin Site

-- Comment Response List

- Content Site #1

- Content Site #2

- Content Site #n

I've got a custom webpar that allows users to comment on a page. I'd like to be able to store the responses in a single list (comment response list).

This is an open site and user's do NOT need to login to browse through the site and make the comments.

What I'm trying to do is get a list reference (SPListCollection) to the Comment Response List from inside one of the Content Sites. The part works fine if the user is at the Portal or Admin Site level, but the 'root' context turns out to be Content Site #x. So referencing the list as /sites/admin returns Sites/Content Site #x/Sites/admin and that doesn't seem right Smile

Code Snippet

SPSite currentSite = SPControl.GetContextSite(this.Context);

SPWebCollection webs = currentSite.AllWebs;

SPWeb currentWeb = webs["Sites/admin"];

SPList list = currentWeb.Lists[sourceList];

SPListItemCollection items = list.Items;

SPListItem item = items.Add();

So, this code only works going down, into a site to get at a list stored lower that current location. I'd like to be able to reference the list using something like:

SPWeb currentWeb = webs["<url to the site>"];

Sean




Re: SharePoint - Development and Programming Cross Site Lists

Curtis Ruppe (MicroStaff IT)

Sean, you were close, instead create an SPSite object using the url to your other site collection:

Code Snippet

SPSite currentSite = new SPSite("htp://server/sites/admin");

SPWeb currentWeb = currentSite.OpenWeb();

SPList list = currentWeb.Lists[sourceList];

SPListItemCollection items = list.Items;

SPListItem item = items.Add();

Just keep in mind, whenever you use the GetContextSite(this.Context), it is grabbing the site collection entrypoint, per the current site. So if a user is logged into Content Site #1, that is a different site collection than the Admin site. There may be security issues here as well. In other words, this current code assumes the NT login information will translate to a user with Contributor access in the admin site.

FYI, the only change was the first two lines of code, which replaces your first three lines of code.





Re: SharePoint - Development and Programming Cross Site Lists

D. Sean Kelly

Thanks! I'll give that a try in the morning. In the mean time I just threw all the data I was gathering into a database. I'd much rather keep the data stored in a list though; easier for the non-developers to deal with.

Update:

Curtis,

Thanks! 5 mins to make the changes and it's working wonderfully. On the security side. Interestingly, they want this portal to be open (no authentication required). So, I'm using impersonation to gain access to that specific part of the portal. That is the reason that the list was put into it's own site. It seems that lists at the portal level cannot have their access set to anonymous, but at the site level they can.

Again, I appreciate the assistance with this one!


Sean