SPContext.Current.Site.Sites object throws an Argument Exception.
-
Friday, September 14, 2012 6:31 AM
Hi,
I have a requirement.
I need to traverse through all the site and subsites URLs under a particular site collection and Put all the URLs in a List in another site collection prgramatically. Please help. Actually I used the below code snippet to achieve but faced an issue like: SPWebCollection sites = siteCollection.AllWebs returns Argument exception.
SPList list = SPContext.Current.Web.Lists["RegisteredIPipeSites"];
foreach (SPSite siteCollection in siteCollections)
{
SPWebCollection sites = siteCollection.AllWebs;
foreach (SPWeb site in sites)
{
SPListItem listitem = list.AddItem();
listitem["SiteCollectionURL"] = siteCollection.Url;
listitem["SiteURL"] = site.Url;
listitem.Update();
}
}Knowledge is power.
All Replies
-
Friday, September 14, 2012 8:55 AM
Have you tried using
SPSecurity.RunWithElevatedPrivileges(delegate() {SPList list = SPContext.Current.Web.Lists["RegisteredIPipeSites"];
foreach (SPSite siteCollection in siteCollections)
{
SPWebCollection sites = siteCollection.AllWebs;
foreach (SPWeb site in sites)
{
SPListItem listitem = list.AddItem();
listitem["SiteCollectionURL"] = siteCollection.Url;
listitem["SiteURL"] = site.Url;
listitem.Update();
}
}});
Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.
- Edited by Hiren.j.Patel Friday, September 14, 2012 8:56 AM
-
Friday, September 14, 2012 9:50 AM
Still no luck. I tried an alternate way as below. I am able to see my all site collections in side "this.WebApplication.Sites" object but it is traversing only one site collection. System is not traversing over all the site collection. Any idea?
SPSiteCollection _sites = this.WebApplication.Sites;
foreach (SPSite site in this.WebApplication.Sites)
{
try
{
SPWebCollection subSites = site.AllWebs;for (int i = 0; i < subSites.Count; i++)
{
SPListCollection lists = subSites[i].Lists;for (int j = 0; j < lists.Count; j++)
{
//SPListItem listitem = list.AddItem();
//listitem["SiteCollectionURL"] = "";
//listitem["SiteURL"] = subSites[i].Title.ToString();
//listitem.Update();
}
}
}
catch (Exception e)
{
}
finally
{
site.Dispose();
}
}Knowledge is power.
-
Friday, September 14, 2012 10:34 AMModerator
Hi,
To return the collection of site collections in a SharePoint Web application, use below code:
SPWebApplication webApplication = SPContext.Current.Site.WebApplication; SPSiteCollection siteCollections = webApplication.Sites; foreach (SPSite siteCollection in siteCollections) { ///// }Check this MSDN to get site/subsite/list/: http://msdn.microsoft.com/en-us/library/ms462161.aspx
Hope it could help
Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
- Marked As Answer by Lhan HanModerator Friday, September 21, 2012 9:09 AM
-
Friday, September 14, 2012 10:56 AM
Hi Hemendra,
My issue is different.I tried your mentioned code still same issue I am facing.I don't see my code and your provided code are of different logic.
My issue is: System traverse only one site collection even thoush the collection shows all the site collection.
Knowledge is power.
- Marked As Answer by Lhan HanModerator Friday, September 21, 2012 9:09 AM
-
Friday, September 14, 2012 11:03 AMcan you please provide the error screen shot
Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.
-
Friday, September 14, 2012 11:21 AMModerator
>System traverse only one site collection even thoush the collection shows all the site collection
What do you mean by site collection show all site collection? Did you verify the site collection in central admin for web application? Go to CA-->Manage web application -->and check how many site collections are existing for your web app.
Bit confusion here so kindly correct us
Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
- Marked As Answer by Lhan HanModerator Friday, September 21, 2012 9:09 AM

