Sharepoint powershell to associate a site template - sharepoint

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
}
}
}
}
}
}

Related

Getting all site collections with csom does not work when created with new office admin portal

I can retrive all site collection information with the following code:
Tenant tenant = new Tenant(ctx);
props = tenant.GetSiteProperties(0, true);
ctx.Load(props);
ctx.ExecuteQuery();
foreach (SiteProperties sp in props) {...}
But when I create a new site collection with the new Office 365 admin portal, the created site collections are not listed in props. I can only see the one created with the old admin portal.
Did I miss sth?
We can use Tenant.GetSitePropertiesFromSharePoint method to achieve it. The Tenant.GetSiteProperties method won't return the Site Collections created on new modern site templates.
Tenant tenant = new Tenant(context);
SPOSitePropertiesEnumerable siteProps = tenant.GetSitePropertiesFromSharePoint("0", true);
context.Load(siteProps);
context.ExecuteQuery();
Console.WriteLine("Total Site Collections: " + siteProps.Count.ToString());
foreach (var site in siteProps)
{
Console.WriteLine(site.Title +"\t"+ site.Template.ToString());
}
Reference: Get All Site Collections from SharePoint Online Tenant

How to get a valid SharePoint web from input URL

Working with a SharePoint 2010 Visual Studio workflow that will copy data from a local farm to a remote farm using web services.
I'm having trouble trying to parse an input URL to get the corresponding web on the remote farm.
Example inputs:
https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx
https://test3.remotefarm.com/
I have no control over what the input will be, so it could be the root web app or five webs deep.
Once I have a valid web URL, I will be appending a web service endpoint.
Edit - This is what I ended up using
Uri fileUrl = new Uri("https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx");
//Websref is a Web Reference pointing to \webs.aspx
websRef.Webs webs1 = new websRef.Webs();
webs1.Url = "https://" + fileUrl.Authority + "/" + sWebsSuffix;
webs1.UseDefaultCredentials = true;
string strWebUrl = webs1.WebUrlFromPageUrl(fileUrl.ToString());
You can use SPSite to get the site collection from the URL and then get the Web app using OpenWeb(). For example:
using (SPSite siteCollection = new SPSite("https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx"))
{
SPWeb myWeb = siteCollection.OpenWeb();
//do stuff with myWeb here
}

SharePoint 2010 unable to get my custom groups

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>");
}
}

How to get all site collections in a web application

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?

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();
}
}

Resources