Configure SharePoint Blocked File Types using PowerShell

A very nice set of Powershell scripts to manage blocked file type for a web application by Ryan Dennis.

Worth to have on your toolbox:

 

See you,
Amadeu.

Deploy Content Files to SharePoint Using SharePoint.DesignFactory.ContentFiles

I’ve been working with SharePoint 2010 for different projects and teams for a while and one thing that has been always a challenge is the source control and publishing process on content items (such as JavaScript, images and CSS files).

Visual Studio SharePoint projects allow us to create modules to deploy these kind of files but it has a big drawback. When you do it, the files are deployed as “file definitions” – ghostable on the libraries. If a user customize the files, any new deployment will not update the instance on your site, only the file definition. If you want to see the updated version of the file you should go to SharePoint Designer and reset the specific file to its definition.

This is pretty bad and makes lots of people confused on why the file wasn’t updated.

I was trying to find a solution for this issue when a found a blog post by Serge van den Oever explaining the installation of a very interesting extension to Visual Studio 2010 to solve this issue. It is called SharePoint.DesignFactory.ContentFiles.

The extension allows you to:

  • deploy content files from your Visual Studio to your local machine using the SharePoint API or to remote machines using the SharePoint client API.
  • map folders on your solution to document libraries and special folders on a SharePoint site so you can deploy files to different locations.
  • deploy files checking them out, in, publishing and approving it.
  • deploy only files that have changed since the last deployment.
  • customize the commands to deploy your solution – check documentation for details.

To install it follow the steps on the article SharePoint.DesignFactory.ContentFiles – Installation. The extension was built using the NuGet tool for Visual Studio. You need to have NuGet installed on your Visual Studio before running the extension. It also needs the MME extension to allow the tool to create custom menus on the project and items right-click menu.

After the installation, your Visual Studio project should contain some extra items:

  • _MachineConfiguration folder: this folder contains Powershell scripts to tell the extension where to deploy the files. By default it will look for a script called default.ps1, then for one with the machine name or for a custom one if specified when the deploy command is called.
  • _Tools folder: this folder contains batch files, Powershell scripts, reference DLLs ,documentation and other support files used by the extension.
  • Mappings.xml: file used to map folders from the Visual Studio project to the document libraries on the SharePoint site.
  • packages.config: Nuget configuration file to tell it to load the extension.

The installation worked just fine for me. I had only to check and tweak a few items to make it work.

After the installation is done, just check if you have a DLL named VSMenuManager.SharePoint.DesignFactory.ContentFiles.dll on your solution folder.

Edit the mappings.xml file to make sure you have all mapped folders created on your Visual Studio project otherwise the deployment will fail.

Create a copy of the Sample.ps1 file on the _MachineConfiguration folder with the name Default.ps1. Configure it with your machine’s web application URL and the settings for the deployment type (object model or client object model), user name, password and claims authentication usage.

After that just add the files you want to deploy to the folders and deploy all files to your web application. The output window can show you the progress of the deployment:

The documentation provides a very good insight on how to use and configure the extension and can be very useful. It can be found under the _Tools\Documentation folder.

If you need to create custom Configurations to deploy to different environments you can do that by following the steps on the Configuration part of the documentation.

In order to get my custom configurations to work I had to edit the Deploy.cmd and the Redeploy.cmd to accept thew configuration name as a parameter.

I added the %1 parameter by the end of line 2 on both files and it worked just fine.

You should test and see how much easier is to deploy content files to SharePoint 2010!

Thanks Serge! The SharePoint.DesignFactory.ContentFiles is really awesome!!!

See you,

Amadeu.

Powershell Week – Day 5: Viewing and Setting SharePoint Diagnostic Logging levels using Powershell

Whenever you install a new SharePoint farm you might go thru that annoying task of setting the ULS and the Event Log levels. This is one of the moments our friend Powershell can give us a good hand.

Follow a few scripts to help you check your current settings and set new values for entire areas or specific sections.

List the main areas of the Log configuration:

get-sploglevel | select-object area | sort-object -property 'area' -unique

List the details of all the areas of the Log configuration:

get-sploglevel | format-table area , name

List the configured log level for one specific area:

get-sploglevel -identity "SharePoint Server:*" | format-table name, eventseverity, traceseverity

Set the log level as Error for the Event Log and Monitorable for the ULS log for one specific area:

set-sploglevel -identity "SharePoint Server:*" -eventseverity Error -traceseverity Monitorable

My suggestion is for you to analyze which services you use on your farm and define the log levels according the importance of the services for your implementation. There is nothing wrong with settings only high severity events on the log if you don’t monitor it regularly and only want to see major issues. If you have a situation where it is important to have the logs in more detailed level, you can set the logs to different levels only when debugging and set it back to the regular level after the issue is solved.

See you,

Amadeu.


Powershell Week – Day 4: Copying Files Between Document Libraries

Day 4 of the Powershell Week is all about copying files from one document library to another.


# Source document library full path
$source = "http://yourwebapplication.com/web/sourcedoclib"
# Destination document library full path
$destination = "http://yourwebapplication.com/web/destinationdoclib"

$sourceList = Get-SPList $source
$sourceFieldCol = $sourceList.Fields;
$sourceItems = $sourceList.GetItems();

$destinationList = Get-SPList $destination

foreach($spListItem in $sourceItems) {
 $newSPListItem = $destinationList.RootFolder.Files.Add($spListItem.Name, $spListItem.File.OpenBinary()).Item;;

foreach($spField in $sourceFieldCol) {
 if ($spField.ReadOnlyField -ne $True) {
 $newSPListItem[$($spField.InternalName)] = $spListItem[$($spField.InternalName)];
 }
 }
 $newSPListItem.Update();
}

Get the script here.
See you,

Amadeu.

Powershell Week – Day 3: List Items With Unique Permission

This script can be used to list all items in a web application with unique permissions or broken permissions inheritance.

It can be useful when trying to diagnose permissions issues on big web application with lots of sites and lists/document libraries.


$webAppURL = "http://yourwebapplication.com"

function CheckItemsWithUniquePermissions() {
 $webapp = Get-SPWebApplication $webAppURL

foreach ($site in $webapp.Sites)
 {
 $allwebs = $site.allwebs
 foreach($web in $allwebs)
 {
 $webURL = $web.url
 write-output "[WEB] Processing Web $webURL"

 foreach ($list in $web.Lists)
 {
 $listName = $list.title
 write-output "[LIST] Processing List $listName"

 foreach ($item in $list.Items)
 {
 $itemTitle = $item.title
 $uniquePermissions = $item.HasUniqueRoleAssignments

 if($uniquePermissions -eq "True")
 {
 write-output "[ITEM] Item '$itemTitle' - Unique Permissions: $uniquePermissions"
 }
 }

 $uniquePermissions = $list.HasUniqueRoleAssignments

 if($uniquePermissions -eq "True")
 {
 write-output "[LIST] List '$listName' - Unique Permissions: $uniquePermissions"
 }
 }

 $uniquePermissions = $web.HasUniqueRoleAssignments

 if($uniquePermissions -eq "True")
 {
 write-output "[WEB] Web '$webURL' - Unique Permissions: $uniquePermissions"
 }
 }
 }
}

CheckItemsWithUniquePermissions

You can get the script file here.

See you,

Amadeu.

Powershell Week – Day 2: Copying Lists

The second post of the Powershell Week is about copying lists. I found this to be very interesting when you have multiple copies of a environment (for several developers, for instance) and needs to synchronize the data structure and content between them.

I based these 2 scripts on the article Copying Lists with PowerShell in SharePoint 2010 from Jerry Orman.

I created 2 scripts:

One to export the data

$sourceWebUrl = "http://yourwebapplicationurl.com"

# path to save the exported data
$path = "c:\data\export\"

#comma delimited list of List names to copy
$lists = @("List1", "List2")

#comma delimited list of Document Libraries to copy
$libs = @("DocLib1", "DocLib2")

foreach($list in $lists)
{
    "Exporting List " + $list
    export-spweb $sourceWebUrl -ItemUrl ("/lists/" + $list) -IncludeVersions All -path ($path + $list + ".cmp") -force
    "Exporting complete."
}

foreach($lib in $libs)
{
    "Exporting Document Library " + $lib
    export-spweb $sourceWebUrl -ItemUrl ("/" + $lib) -IncludeVersions All -path ($path + $lib + ".cmp") -force
    "Exporting complete."
}

And one to import the data.

$destWebUrl = "http://yourwebapplicationurl.com"

# path to load data from
$path = "c:\data\import\"

#comma delimited list of List names to copy
$lists = @("List1", "List2")

#comma delimited list of Document Libraries to copy
$libs = @("DocLib1", "DocLib2")

foreach($list in $lists)
{
   "Importing List " + $list
   import-spweb $destWebUrl -path ($path + $list + ".cmp") -force
   "Importing complete."
}

foreach($lib in $libs)
{
   "Importing Document Library " + $lib
   import-spweb $destWebUrl -path ($path + $lib + ".cmp") -force
   "Importing complete."
}

I hope you find it useful as well.

See you,
Amadeu.

Powershell Week – Day 1: Copy List Structure Using Template

This week I’m starting a series of posts with several Powershell scripts I’ve been writing to help with some activities that might take a long time if you do it manually. I hope you enjoy the Powershell scripts as I do!

This first one is about creating a list or document library based on an existing one. The approach used is to save the existing library as a template and then creating the new one based on the template.

$site = get-spsite("http://www.yourwebapplication.com")
$web = $site.Allwebs["your web site"]
$list = $web.Lists["your library"]
$list.SaveAsTemplate("TemplateName.stp", "TemplateName", "Template Name", $false)
# the last parameter defines wheter you want the data to be saved in the template

$listTemplates = $site.GetCustomListTemplates($web)
$web.Lists.Add("New List URL", "New list Name", $listTemplates["TemplateName"])

If you want, you can also delete the template after the copy is created.

See you,

Amadeu.

Interesting Article on XsltListViewWebPart Performance

Jaap Vosser wrote a very interesting and explanatory article on the XlstListViewWebPart performance:

How the XsltListViewWebPart in SharePoint 2010 can be a real performance killer

It gives us good reasons to pay more attention to the use of this web part, how it affects the page’s performance when you have more than a web front-end.

Have a good reading!!!

 

See you,

Amadeu.

SharePoint 2010: The security validation for this page is invalid

Deploying a new subsite into a SharePoint 2010 site collection, using a custom master page we started seeing the following error message when trying to add users to groups and changing settings of document libraries/lists:

The security validation for this page is invalid.

 

Reverting the master page back to the default one, the operations worked just fine. So it seemed the master page was missing something. Searching on Google about it I found 3 interesting articles about this subject:

http://www.simple-talk.com/community/blogs/charleslee/archive/2010/01/05/85440.aspx

http://snahta.blogspot.com/2008/11/security-validation-for-this-page-is.html

http://techtrainingnotes.blogspot.com/2009/12/sharepoint-security-validation-for-this.html

 

Basically, they explain this error is caused by a lack of validation on pages which change the content database. It can be solved in 2 ways:

-Adding the AllowUnsafeUpdates to your custom code.

SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;

-Adding the FormDigest Sharepoint control to your master page.

From the MSDN article:

“To make posts from a Web application that modify the contents of the database, you must include the FormDigest control in the form making the post. The FormDigest control generates a security validation, or message digest, to help prevent the type of attack whereby a user is tricked into posting data to the server without knowing it. The security validation is specific to a user, site, and time period and expires after a configurable amount of time. When the user requests a page, the server returns the page with security validation inserted. When the user then submits the form, the server verifies that the security validation has not changed.”

 

The second solution solved our issues and the pages worked fine!!!

 

See you,

Amadeu.