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>");
}
}
Related
I have a scenario in my custom visual web part where I need to check for logged in User is a member of sharepoint group(sharepoint groups or users are stored in a sharepoint list). Actually if logged in users exists in the list, he will be given Edit access in my custom web part.
Since I have created a group name "SharePoint_Owners" with group settings as 'Who can View Membership of this group' to 'Group Members', Site is throwing error as 'Access denied' as logged in user doesn't have permission to view. I get error when my code executes this,
SPGroup oGroup = oWebsite.SiteGroups[strgroup];///strgroup is a group name
foreach (SPUser oUser in oGroup.Users) { }
Site throws this error when I try to open page which consists my webpart.
Can any one suggest me how do i proceed? is there a way to resolve this programmatically without actually giving View permission to "Everyone" for each group??
I thought RunWithElevatedPrivileges does my work but have no luck!
please help
Try this:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string siteURL = SPContext.Current.Site.Url;
using (SPSite safeSite = new SPSite(siteURL))
{
using (SPWeb safeWeb = safeSite.OpenWeb())
{
SPGroup group = safeWeb.Groups["SharePoint_Owners"];
bool isMember = safeWeb.IsCurrentUserMemberOfGroup(group.ID);
}
}
});
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 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.
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;
}
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();
}
}