Archive for December, 2009

Create and delete SharePoint list views with PowerShell

Here’s how you can create and delete views in a SharePoint list or library using PowerShell (Is there anything that can’t be done through a PowerShell script?)

The script below creates a view called “TestView”.  It expects three command-line arguments:  site collection URL, the name of the view to create, and the list GUID.  The view that’s created is an exact replica of the “All Items” view (you can certainly modify the code as needed).  Here’s how you would call this script from the command line:


powershell  CreateView.ps1 "your_site_collection_URL" "TestView" "List GUID 6865306f-60e0-4889-addd-4fb9862e72e0"

Script code (use the button in the top right corner to copy it to the clipboard):


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

# reading command line arguments

$siteURL = $args[0]

$strViewName = $args[1]

$ListGUID = $args[2]

# enter your CAML query for the view here...

$strQuery = "<Where><Gt><FieldRef Name='ID'/><Value Type='Counter'>0</Value></Gt></Where>"

 # create a new SPsite object and recursively go through all webs

# until a matching list GUID is found

$site=new-object Microsoft.SharePoint.SPSite($siteURL)

foreach ($web in $site.AllWebs)
{
 

foreach ($list in $web.Lists)
{

$ListTempGUID = $list.ID.ToString()
  

if ($ListTempGUID.Contains($ListGUID))
{

write-host "**********************************************"
write-host "Match found. Preparing to create a view: ", $strViewName
write-host "List Title: ", $list.Title
write-host "List GUID: ", $list.ID

$fields = $list.Views["All Items"].ViewFields.ToStringCollection()

$result = $list.Views.Add($strViewName, $fields, $strQuery, 100, $True, $False , "HTML", $False)

write-host "View ", $strViewName , " was created successfully."

break

}
}

} 

write-host "Done."

$site.Dispose(); ##ENFORCED DISPOSAL!!!

    

So far, so good?  Well, now that you’ve created a view, how do you delete it?  Follow the same logic, only instead of using the Add method of SPViewCollection object, we’ll be using the Delete method.

Same command line arguments as before:  site collection URL, name of the view, and list GUID.

Calling script from the command line:


powershell  DeleteView.ps1 "your_site_collection_URL" "TestView" "List GUID 6865346f-60e0-4889-addd-4fb3862572e0"

 


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

# reading command line arguments...

$siteURL = $args[0]

$strViewName = $args[1]

$ListGUID = $args[2] 
# creating a new site object and recursively searching through its lists

# until a matching list GUID is found

$site=new-object Microsoft.SharePoint.SPSite($siteURL)

foreach ($web in $site.AllWebs)
{
 

foreach ($list in $web.Lists)
{

$ListTempGUID = $list.ID.ToString()
  

if ($ListTempGUID.Contains($ListGUID))
{
foreach ($view in $list.Views)
{

If ( $view.Title.Contains($strViewName))
{

write-host "**********************************************"
write-host "Match found. Preparing to DELETE a view: ", $view.Title
write-host "List Title: ", $list.Title
write-host "List GUID: ", $list.ID

# you can insert a pause here if you like...

$list.Views.Delete($view.ID)

write-host "View ", $view.Title , " has been deleted successfully."

break

}
}

break

}

}

}
write-host "Done."

$site.Dispose(); ##ENFORCED DISPOSAL!!! 
 

Finally,  how do you find out the GUID of your list?  It’s fairly straightforward – you just need to access the SPList.ID property of your list.  Here’s a simple script that will output the GUIDs of all lists in your site collection:


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
# write column headings

write-host "List URL ; Web URL ; Web Title ; List Title; List GUID ;"

# create a site object and recursively list all of its list objects and their details
 $site=new-object Microsoft.SharePoint.SPSite("http://your_site_collection_URL)  

  foreach ($web in $site.AllWebs)
 {
  foreach ($list in $web.Lists)
 {
 write-host $list.DefaultViewURL, ";", $web.URL , ";",  $web.Title , ";" , $list.Title , ";" , $list.ID

 }

 }

$site.Dispose(); ##ENFORCED DISPOSAL!!!

Advertisements

Calling STSADM from within a PowerShell script

For various SharePoint admin tasks, there are times when you need to execute an STSADM command from within a PowerShell script. 

Before you can do that, you’ll need to tell PowerShell where to find STSADM executable. You can do that by adding the following line to your profile.ps1 file (usually located in C:\WINDOWS\system32\windowspowershell\v1.0 ):

Set-Alias -Name stsadm -Value $env:CommonProgramFiles”\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE”

Now you can use stsadm in your scripts and it should work beautifully…

Content Deployment error: Maximum request length exceeded.

Sometimes, when you run a Content Deployment job between a source farm and a destination farm, you may encounter the following issue:

“Content deployment job ‘Job Name’ failed. The remote upload Web request failed.”

When you look in the ULS logs, you may see the following:

File upload of ‘C:\Inetpub\wwwroot\SiteDirectory\ExportedFiles70.cab’ failed. Exception System.Web.HttpException: Maximum request length exceeded.  at System.Web.HttpRequest.GetEntireRawContent()     at System.Web.HttpRequest.get_InputStream()… 

OK, let’s do a web search to see how to fix this issue.  After some searching, you’ll come across posts that tell you to modify maxAllowedContentLength or MaxRequestLength attribute in the Central Administration web.config file (or maybe WFE’s too).  Examples: 

http://technet.microsoft.com/en-us/library/dd795107.aspx

http://weblogs.asp.net/hosamkamel/archive/2007/09/18/resolving-maximum-request-length-exceeded-exception.aspx

I’ve found that if you only modify web.config files as specified in the articles above, it will NOT solve the problem.  

 To solve this problem, you need to increase maxRequestLength value in three places on the TARGET farm:  

  1. Central Administration web.config file (typically located in C:\Inetpub\wwwroot\wss\VirtualDirectories\DirectoryName)
  2. Your web application main web.config file (typically located in C:\Inetpub\wwwroot\wss\VirtualDirectories\DirectoryName).
  3. Content deployment web.config file located in:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\ADMIN\Content Deployment

Note:  You can find out the correct paths for items 1 and 2 by opening IIS Manager.

Open web.config file located at each location and locate the maxRequestLength attribute.  Increase it to allow upload of the largest CAB file that you have.  The default setting limits the upload file size to 51200 KB for CA and web application and to 102400 KB for content deployment.

<configuration>
  <system.web>
    <httpRuntime maxRequestLength=”102400″ />
  </system.web>
</configuration>

You can find out how large the largest CAB file is by running the following command on the source farm Central Administration server:

stsadm -o editcontentdeploymentpath -pathname (pathname here) -keeptemporaryfiles Failure

 This setting will allow you to see the CAB files generated during the content deployment job (in the temp directory specified in the Content Deployment settings screen in Central Administration).

Modify web.config file to display detailed error messages

I was dealing with another one of those “helpful” SharePoint error messages today:

“An unexpected error has occurred.”

Nishant Rana’s blog post had the exact steps needed to get SharePoint to spit out the details:

http://nishantrana.wordpress.com/2009/03/24/%E2%80%9Can-unexpected-error-has-occurred%E2%80%9D-error-message-in-sharepoint/

I’m bookmarking this one!

The following two web.config files will need to be updated:

  • C:\inetpub\wwwroot\wss\VirtualDirectories\<web app name>\web.config
  • C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\web.config

Find out the following entry

<SafeMode MaxControls=“200“ CallStack=“false“ DirectFileDependencies=“10“ TotalFileDependencies=“50“ AllowPageLevelTrace=“false“>

And make following changes to it

 <SafeMode MaxControls=“200“ CallStack=“true“ DirectFileDependencies=“10“ TotalFileDependencies=“50“ AllowPageLevelTrace=“true“>

 <customErrors mode=“On“ />

To

<customErrors mode=“Off“ />


RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS InfoPath Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 353,733 hits