I want to list all site collections under my web application; the purpose is to list all mysites created by users.
Like that:
/members/sites/sqladmin
/members/sites/sqluser
/members/sites/sqluser1
/members/sites/sqluser2
/members/sites/user1
For the purpose of accessing all site collections within a given web application there's, quite naturally, the SPWebApplication.Sites property. To get access to a particular web application you can use a code like this:
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://my-web-app-url:80/"));
(see MSDN here as well).
Use SPWebApplication.Sites property for that.
On the Central Administration home page, click Application Management.
On the Application Management page, in the Site Collections section, click View all site collections.
The Site Collection List page lists all the site collections in the Web application.
Use below code to programmatically get the all site collections of the web application.
url parametter = web application url.
public DataTable GetAllSiteCollections(string url)
{
DataTable dt = new DataTable();
dt.Columns.Add("URL");
dt.Columns.Add("Name");
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(url));
foreach (SPSite site in webApplication.Sites)
{
dt.Rows.Add(new object[] { site.Url, site.Url });
}
});
return dt;
}
Related
During database detach and attach process, all the sites are migrated to the new 2010 farm but are not yet associated with any site template. I want to be able to run through all the sites that do not have any site template associated yet and are just sitting there in the content DB and then be able to assign them a site template like a team site.
Is it possible to accomplish this using powershell in sharepoint?
Someone please give me their insights here..
You can loop through site collections if you access SPFarm.Local.Services. Then you have to look for web application objects and in each of them you can look for sites.
In c# code you can do it like this:
SPFarm farm = SPFarm.Local;
foreach (SPService objService in farm.Services) {
if (objService is SPWebService) {
SPWebService webService = (SPWebService)objService;
foreach (SPWebApplication webApp in webService.WebApplications) {
foreach (SPSite site in webApp.Sites) {
foreach (SPWeb web in site.AllWebs) {
if (web.Provisioned == false) {
//...whatever
}
}
}
}
}
}
I have created a couple of groups in my SharePoint 2010 publishing site. I have some application pages where I'm trying to get
using (SPSite spsite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb curWeb = spsite.OpenWeb())
{
SPGroupCollection groups = curWeb.Groups;
}
}
It shows existing (default) site groups but not my groups.
Please suggest some solution with problem definition. I don't know why this is happening because when I try to access any of my lists using the GetList("list name") function, I never get that whereas if I try to access it through indexer like Lists["list name"] I get that list.
Somehow I can access lists of my site collection but not of the current website.
I'm not sure if there are some rights/permissions issues or what the issue could be. Please provide references if possible.
If you have a SPContext, you can use SPContext.Current.Site.WebApplication.Sites to get at this list of site collections. From the SPSite, there is the AllWebs property for all webs and SiteGroups of the RootWeb for the list of groups. Please remember that there is no notion of a group in a sub-site
OR
using (SPWeb oWebsite = SPContext.Current.Site.URL)
{
SPGroupCollection collGroups = oWebsite.Groups;
foreach (SPGroup oGroup in collGroups)
{
Response.Write(SPEncode.HtmlEncode(oGroup.Name) + "<BR>");
}
}
I have written a piece of code to create a custom security group in a SharePoint app. the code runs on feature activation at Site level and is as follows:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = (SPSite)properties.Feature.Parent;
using (SPWeb web = site.OpenWeb())
{
if (!GroupExists(web.SiteGroups, "Test Column Administrators"))
{
web.SiteGroups.Add("Test Administrators", web.AssociatedOwnerGroup, null, "Contains users and groups who can administer Test Column articles.");
web.AssociatedGroups.Add(web.SiteGroups["Tets Column Administrators"]);
web.Update();
}
}
}
The code does create that group and adds it to the SharePoint site however when I go to Site Actions->Site Permissions (_layouts/user.aspx page), that group is missing. But when I manually go to the groups.aspx page (_layouts/groups.aspx) it is there.
How can I get my code to create that group in such a way that it appears in the user/aspx page as well?
Thanks in advance
That is completely OK. The Groups page displays the list of groups that actually exist in the Site. And the page Users.aspx displays what Permissions the principals have within this Site. Your code is OK but you have to add more code that grants permissions to your group if it needs permissions. When your group has permissions within the site it will appear on the Users.aspx page. See a sample how to add permissions to and item, same is for Site level and web level.
I use the sharepoint lists as a database.
I want to somehow impersonate as different user inside the webpart code
and than as this user I will have both write and edit permission to the list.
My goal is to be able to have full premission only through the webpart code.
I am using MOSS 2007.
SPSecurity.RunWithElevatedPrivilieges() will execute your code as the system account, i.e. the account under which the application pool runs, which might or might not be what you want to do. For example, if you have a workflow attached to the list which is supposed to trigger when new items are added to the list, it will not fire if you insert a new list item under the credentials of the system account (this was a security fix introduced in SharePoint 2007 SP 1). In that case you will have to perform the insert operation under a different account that has the correct permissions on the list.
You can get the UserToken for any user using the following code:
SPUserToken userToken = null;
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
userToken = web.AllUsers["domain\\username"].UserToken;
}
}
});
Replace the "domain\username" with the correct windows account name. Then you can pass this user token to one of the overloads of the SPSite object constructor to execute the code under this user's credentials like so:
using (SPSite site = new SPSite(SPContext.Current.Site.ID, userToken))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
// This code will execute under the credentials of the userToken user
}
}
Hope this helps.
You are looking for SPSecurity.RunWithElevatedPrivileges Method.
How to get all sites and sub-sites in SharePoint and access an image library/list?
I am looking forward to achieve this via the SharePoint object model. Inside each site or subsite I want to access an image library/list,
After getting to this list, how do I set the option of 'Required Content Approval for Selected Items' from 'Yes' to 'No'?
Use the SPFarm object to get all web applications then use SPWebApplication to get all sitecollection and then use SPSite to get all Webs.
You have to loop through all three to get all sites under the site collection. If you want to find subsites under spweb please call all spwebs recursively until you don't find any webs under spweb for each spweb.
SPFarm farm = SPFarm.Local;
SPWebService service = farm.Services.GetValue<SPWebService>("");
foreach (SPWebApplication webapp in service.WebApplications)
{
foreach (SPSite sitecoll in webapp.Sites)
{
foreach (SPWeb web in sitecoll.AllWebs)
{
<<Use recursion here to Get sub WebS>>
web.Dispose();
}
sitecoll.Dispose();
}
}