Pages

Wednesday 30 January 2013

How to list all the Web Applications, all the site collections within the web app and all the Level 1 sub sites within that site collection using c# SharePoint 2010

I recently had  a requirement for a SharePoint Hosting provider, they wanted an inventory list of all the web applications, all the site collections and the first level sub sites with the site collection.

I used a console application, you can do this easily with Powershell as well.


try
            {
                SPServiceCollection services = SPFarm.Local.Services;

                foreach (SPService curService in services)
                {
                    if (curService is SPWebService)
                    {
                        SPWebService webService = (SPWebService)curService;
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                foreach (SPWebApplication webApp in webService.WebApplications)
                                {
                                    // here you can do something with webApp

                                    foreach (SPSite siteCollection in webApp.Sites)
                                    {
                                        using (SPSite site = new SPSite(siteCollection.Url.ToString()))
                                        //(ConfigurationManager.AppSettings["SiteCollUrl"]))
                                        {
                                            SPWebCollection webCollection = site.AllWebs;
                                            if (webCollection.Count > 0)
                                            {
                                                for (int i = 0; i < webCollection.Count; i++)
                                                {
                                                    if (!webCollection.Names[i].Contains("/"))
                                                    {
                                                        Console.WriteLine(webCollection[i].Url + " : " + webCollection[i].Title + " : " +
                                                                            webCollection[i].Description + " : " + webCollection[i].IsRootWeb);
                                                    }
                                                }
                                            }
                                        }
                                        //}
                                        siteCollection.Dispose();
                                    }
                                }
                            });
                    }
                }
            }
            catch (Exception ex)
            {
                writetoCSV("", "", ex.Message, false);
                throw ex;
            }


You can write this to a CSV file or txt file.

If you want a list of all the site collections and sub sites for a given web application use the powershell script listed here:



Tuesday 22 January 2013

Custom 404 error page does not render in Firefox and Chrome but works in IE


Had an issue last week where our custom 404 error page rendered fine in Internet Explorer but did not render in Firefox and Chrome.

Thanks to a colleague of mine, he stumbled across this awesome post by Brian farm hill.

http://blog.brianfarnhill.com/2012/12/issues-with-a-custom-404-page-in-sharepoint-2010

How to activate a set of standard list of features in all site collections for a web application using Powershell in SharePoint 2010

Problem

The department has a site collection that is heavily used and is a key site collection.The business would like to standardise site collection features across the board. 
How to activate a standard list of features in all site collections for a web application using powershell script for SharePoint.

Solution:
Note: # is a comment in powershell

Contents of  ManualListofFeaturesInEDSite.txt - This contains all the features that need to be activated. If you do not wish to read from a text file and use a reference site then just use 

$allSiteColFeature = Get-SPFeature -site "http://muwebapp/sites/nintex" | Select displayname, id 
NintexWorkflow
NintexWorkflowWebParts
NintexWorkflowInfoPath
NintexWorkflowEnterpriseWebParts
NintexWorkflowContentTypeUpgrade
OfficeWebApps
OpenInClient
ReviewPublishingSPD1033
ReportServer
Reporting
PublishingSite
BaseSite
InPlaceRecords
LocationBasedPolicy
WAReports
DocumentSet
DocId
Workflows
LocationBasedPolicy
ViewFormPagesLockDown


 ***** Script begins here ******
  Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\Admin\SiteFeaturesoutput.txt -append

 $webs =  get-spsite -webapplication "http://mywebapp" -Limit All
 # Returns a detailed list of features, hence not being used
 #$allSiteColFeature = Get-SPFeature -site "http://muwebapp/sites/nintex" | Select displayname, id
 
 $fileContent = Get-Content "C:\drive\PROJECTS\DEVELOPMEN\ManualListofFeaturesInEDSite.txt"
 

#$oneweb = "http://mywebapp/sites/nintex"
   
    Foreach ($oneweb in $webs)
    {
        Foreach ($featureId in $fileContent)
        {
            #$siteFeature = get-spfeature -site $oneweb | Where {$_.id -eq $featureId.id}
            $siteFeature = get-spfeature -site $oneweb | Where {$_.displayname -eq $featureId.displayname}
           
            if ($siteFeature -eq $null)
            {
              Write-Host "Activating Site level Features at $oneweb FeatureID: $featureid" -foregroundcolor Yellow
               Enable-SPFeature $featureId -URL $oneweb.URL -Confirm:$False
              #Disable-SPFeature $featureId -URL $oneweb.URL -Confirm:$False
              #Enable-SPFeature $featureId -URL $oneweb -Confirm:$False
            }
            else
            {
              Write-Host "Feature $feature is already activated on $oneweb" -foregroundcolor green
            }
        }

    }

Stop-Transcript



****** End of Script ******

Using PowerShell script to change a web application's authentication to claims mode

Open PowerShell command window and execute the script below:

$webApp = Get-SPWebApplication "http://mywebapp"
$webApp.UseClaimsAuthentication = "True";
$webApp.Update();
$webApp.ProvisionGlobally();

Once the script has executed, navigate to your web app’s authentication provider settings:

image

Under Zone click on Default and select your identity provider for claims based authentication:

image