Export-SPMetadataWebServicePartitionData Permissions

I have used Export-SPMetadataWebServicePartitionDat many times to export metadata from SP 2010 along with all the Guids, but just came across an issue where I got an “Access to the path is denied” error. I made sure the account had all the correct permission in SQL Server and to the local directory where I was trying to saved the exported metadata to but still got the error. After looking in the SP logs I noticed something about GetTempFileName failing.

So I went to the Temp directory and the sercver which it turns out had been locked down and the system account did not have permissions on it. After granting Full Control to the folder (probably overkill but by this point I has wasted too much time already), the export worked

Obviously the script must try and create temp files during the export so ensure you have permission in the Temp dir to avoid the error

Custom Publishing HTML Site Column

I recently needed to create a custom publishing page layout with a number of Publishing HTML site columns in the content type.

I followed all the usual instructions and had field elements marked up as follows;

Type=”HTML”

RichText=”TRUE”

RichTextMode=”FullHtml”

However when I created pages with this content type I noticed it was not quite right

Firstly when clicking on Edit Properties, the ribbon in the popup would blank out when I clicked on the HTML columns, and although I could enter in HTML, when I tried to save the page it said I could not have more than 255 characters in the column

Also if you edited the page from Site actions, although the ribbon let you edit the HTML content in the correct way, when the content was saved I was still seeing the html markup.

To cut a long and painful story short, I added Sealed=True” to the definition and suddenly it all worked as expected.

I will leave it up to someone more clever than myself to work out why this is the case

Hopefully this will save someone else having the same frustrating experience as I did

SharePoint 2010 site creation with PowerShell

I have included below a Powershell script I have written to provision a site structure, along with templates, permissions, navigation, themes and master pages all based on an XML input document.

 When working in a setup with multiple environments such as development, UAT, staging, production etc it is very useful to be able to provision such a structure in a repeatable manner by the use of such scripts, with the ability to change settings simply by editing an xml document

I have noticed a number of Powershell scripts out there which set the theme with the ApplyTheme method, but this is being replaced by the ThmxTheme.SetThemeUrlForWeb method in 2010. Although I saw exampoles for using this within .NET code, I did not see an example of using it with Powershell, hence why I added it here

You would call the script with a command like

CreateSiteStructure “http://<myserver>” “C:\<myFolderPath>\SiteStructure.XML”

 Anyway, here is the script. Hope it is useful to someone on day;

function SetWebTheme($web, $themeName)
{
 $themeUrl = “/_catalogs/theme/” + $themeName + “.thmx”
 [Microsoft.SharePoint.Utilities.ThmxTheme]::SetThemeUrlForWeb($web, $themeUrl)
 $web.Update()
}

function SetWebMasterPages($web, $masterPage, $customMasterPage)
{
 if ($customMasterPage.Length -gt 0)
 {
  $customMasterUrl = “/_catalogs/masterpage/” + $customMasterPage
  $web.CustomMasterUrl = $customMasterUrl
 }

 if ($masterPage.Length -gt 0)
 {
  $masterUrl = “/_catalogs/masterpage/” + $masterPage
  $web.MasterUrl = $masterUrl
 }

 $web.Update()
}

function CreateSitesFromXML($XMLNode, $parentWebUrl, $web)
{
 $parametersOK = “true”
 
 if ($XMLNode.url.Length -eq 0)
 {
  Write-Host “Invalid url for web”
  $parametersOK = “false”
 }
 $newWebUrl = $parentWebUrl + “/” + $XMLNode.url

 $newWebTemplate = “”
 if ($XMLNode.template.Length -gt 0){$newWebTemplate = $XMLNode.template}
 
 $newWebName = “”
 if ($XMLNode.name.Length -gt 0){$newWebName = $XMLNode.name}
 
 $newWebTheme = “”
 if ($XMLNode.theme.Length -gt 0){$newWebTheme = $XMLNode.theme}

 $newWebMasterPage = “”
 if ($XMLNode.masterPage.Length -gt 0){$newWebMasterPage = $XMLNode.masterPage}

 $newWebCustomMasterPage = “”
 if ($XMLNode.customMasterPage.Length -gt 0){$newWebCustomMasterPage = $XMLNode.customMasterPage}

 $newWebDescription = “”
 if ($XMLNode.description.Length -gt 0){$newWebDescription = $XMLNode.description}
 
 $newWebQuickLaunch = $false
 if ($XMLNode.quickLaunch -ieq “true”){$newWebQuickLaunch = $true}
 
 $newWebUniquePermissions = $false
 if ($XMLNode.uniquePermissions -ieq “true”){$newWebUniquePermissions = $true}
 
 $newWebTopNav = $false
 if ($XMLNode.topNav -ieq “true”){$newWebTopNav = $true}
 
 $newWebParentNav = $false
 if ($XMLNode.parentNav -ieq “true”){$newWebParentNav = $true}

 if ($parametersOK -eq “true”)
 {
  if ($newWebTopNav -eq $true)
  {
   if ($newWebTemplate.Length -gt 0)
   {
    New-SPWeb -Url $newWebUrl -Template $newWebTemplate -Name $newWebName -Description $newWebDescription -AddToTopNav
   }
   else
   {
    New-SPWeb -Url $newWebUrl -Name $newWebName -Description $newWebDescription -AddToTopNav
   }
  }
  else
  {
   if ($newWebTemplate.Length -gt 0)
   {
    New-SPWeb -Url $newWebUrl -Template $newWebTemplate -Name $newWebName -Description $newWebDescription
   }
   else
   {
    New-SPWeb -Url $newWebUrl -Name $newWebName -Description $newWebDescription
   }
  }
  
  $newWeb = Get-SPWeb $newWebUrl
  
  $newWeb.QuickLaunchEnabled = $newWebQuickLaunch
  $newWeb.Navigation.UseShared = $newWebParentNav
  $newWeb.Update()

  # Set Master Pages
  if ($newWebMasterPage.Length -gt 0 -or $newWebCustomMasterPage.Length -gt 0)
  {
   SetWebMasterPages $newWeb $newWebMasterPage $newWebCustomMasterPage
  }

  # Set theme
  if ($newWebTheme.Length -gt 0)
  {
   SetWebTheme $newWeb $newWebTheme
  }

  # Set individual permissions
  if ($newWebUniquePermissions -eq $true)
  {
         $newWeb.BreakRoleInheritance($false)
  
   if ($XMLNode.permissions.ChildNodes.Count -gt 0)
   {
    foreach ($permissionNode in $XMLNode.permissions.ChildNodes)
    {
     $accountType = $permissionNode.accountType
     $accountName = $permissionNode.accountName
     $permissionMask = $permissionNode.permissionMask
                    $account = $null
     if ($accountType -eq “SPGroup”)
     {
                        if ($newWeb.SiteGroups[$accountName])
                        {
                            $account = $newWeb.SiteGroups[$accountName]
                        }
                        else
                        {
                            $account = $newWeb.Site.RootWeb.SiteGroups.Add($accountName,$web.CurrentUser,$web.CurrentUser,”")
                            $newWeb.Site.RootWeb.Dispose()
                        }
     }
     else
     {
                        $account = $newWeb.EnsureUser($accountName)
     }
                    $roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
                    $roleDefinition = $newWeb.RoleDefinitions[$permissionMask]
                    $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
                    $newWeb.RoleAssignments.Add($roleAssignment)
    }
   }   
  }

  $newWeb.Dispose()

  # Process child nodes
  if ($XMLNode.sites.ChildNodes.Count -gt 0)
  {
   foreach ($childNode in $XMLNode.sites.ChildNodes)
   {
    CreateSitesFromXML $childNode $newWebUrl $web
   }
  }
 }
}

function CreateSiteStructure()
{
    Param (
            [parameter(Mandatory=$true)][string]$StartSite,
            [parameter(Mandatory=$true)][string]$XMLLocation
          )

 #Get XML file containing groups and associated users
 $sitesXML = [xml] (Get-Content ($XMLLocation))

 $site = Get-SPSite $StartSite
 $web = $site.RootWeb

 #Loop through top level nodes
 $sitesXML.rootNode.sites.site | ForEach-Object {
  CreateSitesFromXML $_ $web.url $web
 }

 $web.Dispose()
 $site.Dispose()
}

Populating SharePoint 2010 Groups with AD Security groups from XML using PowerShell

I have recently had the task of creating a PowerShell script to automatically create SharePoint groups and populate them with AD security groups from an XML input file

I saw a very useful post from Phil Childs showing how to create groups and add users to them but wanted to add AD security groups, and saw a number of questions in the news groups where people had struggled with this.

I also wanted to reformat it as a function so it could be built into a larger script

The secret seems to be to add the AD security group to the SPWeb.AllUsers collection before adding them to the SPGroup.Users collection

The Groups.XML file used to drive the function is shown below;

<?xml version=”1.0″?>

<Groups>

  <Group name=”SPGroup1″ description=”First SharePoint Group”>

    <Users>

      <User name=”WINSMARTS\Chris”></User>

    </Users>

    <ADGroups>

      <ADGroup name=”WINSMARTS\Group1″ displayName=”ADGroup1″></ADGroup>

    </ADGroups>

  </Group>

  <Group name=”SPGroup2″ description=”Second SharePoint Group”>

    <Users>

    </Users>

    <ADGroups>

      <ADGroup name=”WINSMARTS\Group2″ displayName=”ADGroup2″></ADGroup>

    </ADGroups>

  </Group>

</Groups>

The PowerShell script is;

function CreatePopulatedGroupsFromXML

{

    Param (

            [parameter(Mandatory=$true)][string]$StartSite,

            [parameter(Mandatory=$true)][string]$XMLLocation

          )

         #Get Site and Web objects

         $site = Get-SPSite $StartSite

         $web = $site.RootWeb

         #Get XML file containing groups and associated users

         $groupsXML = [xml] (Get-Content ($XMLLocation))

         #Walk through each group node defined in the XML file

         $groupsXML.Groups.Group | ForEach-Object {

                 #Check to see if SharePoint group already exists in the site collection

                 if ($web.SiteGroups[$_.name] -eq $null)

                 {

#If the SharePoint group doesn’t exist already – create it from the name and description values at the node

                          $newGroup = $web.SiteGroups.Add($_.name, $web.CurrentUser, $null, $_.description)

                 }

                 #Get SharePoint group from the site collection

                 $group = $web.SiteGroups[$_.name]

                 #Add the users defined in the XML to the SharePoint group

                 $_.Users.User | ForEach-Object {

                          $group.Users.Add($_.name, “”, “”, “”)

                 }

                 #Add the AD Groups defined in the XML to the SharePoint group

                 $_.ADGroups.ADGroup | ForEach-Object {

                          if ($web.AllUsers[$_.name] -eq $null)

                          {

                                  $web.AllUsers.Add($_.name, “”, $_.displayName, “”)

                          }

                          $group.Users.Add($_.name, “”, “”, “”)

                 }

         }

         #Dispose of Web and Site objects

         $web.Dispose()

         $site.Dispose()

}

You would then call this with a command like;

CreatePopulatedGroupsFromXML -StartSite “http://<MySite>&#8221; -XMLLocation “C:\<MyFolder>\Groups.XML”

SharePoint 2010 Theming

As some of you will be aware, SharePoint 2010 theming has undergone a big rework since 2007

Themes themselves are now shared with the rest of the Office 2010 suite and essentially consist of thmx files containing a series of colours and fonts

Microsoft have then edited many of the OTB SharePoint CSS files to have special markup in them to tell the engine to replace certain colours and fonts with those found in the current theme for the site in question. There is also a RecolourImage operation to change the colouring of specific images.

When it comes to theming a site, tools such as PowerPoint 2010 allow easy creation of the thmx file, which is imported into the themes gallery

The developer then needs to know which of the colours and fonts in the theme file will replace which element and property in the new marked up CSS files

After trying for some time to find some sort of mapping between CSS element and Theme element and failing miserably, I decided to write some code to parse each of the OTB CSS files and pull out the information into a readable format

The list is the result of running this code

It has the following columns;

CSS File: Which CSS file contains this markup

CSS Class: Which Class in the file has this markup

Theme Operation: ReplaceColour or RecolourImage

Theme Element: Which element in the thmx file is to be used for the colour/font in question (Light1, Light2, Dark2-Medium, Accent3-Darker etc)

CSS Property: What property of the class in question is being changed by the theme, (color, border, background-color etc)

Additional Information: Some markup contains further information such as tinting or including additional rectangles in the style. I have not gone into understanding these in depth, but have shown the additional markup anyway

If you are ever in the situation where you need to theme certain elements of a SharePoint 2010 site and do not want to have to create custom CSS files, hopefully this spreadsheet will help identify which elements of the thmx file to colour which colour to get the desired effect.

To improve the performance of this page, the table is now found here

Follow

Get every new post delivered to your Inbox.