Tag Archives: PowerShell

SharePoint 2010: Add a file to the root of your site using PowerShell

This can be useful when you need a file to be right off the root of your Internet facing site – files like robots.txt, sitemap.xml, or the verification file for Google Webmaster tools. We’ll take advantage of PowerShell’s ability to use any .NET methods along with the Files collection on each SPWeb in SharePoint.

$fileBytes = [system.io.file]::ReadAllBytes("c:\the\full\path\to\your\file.txt");
$site = Get-SPSite "http://yourdomain:portifneeded";
$site.RootWeb.Files.Add("file.txt", $fileBytes, $true);

This will result in a file.txt located at “http://yourdomain:portifneeded/file.txt”. Sweet!

SharePoint 2010: Finding the largest document library in a site collection

SharePoint 2007 came with a page (storman.aspx) dedicated to showing you how much space each of the lists in your site collection were taking up. SharePoint 2010 removed this page. Luckily, SharePoint 2010 SP1 added it back in. But what if you’re still haven’t updated to SP1 and you’re getting warnings/errors about running out of space?

Obviously – up the space so as to avoid additional noise from your users. Then – figure out which libraries are taking up the most space. This can be done by using the (now obsolete – but working) StorageManagementInformation method off of SPSite. You can write some C# to use it, or you can use PowerShell.

The required arguments for this method (listed below) can be found by looking at the above MSDN link. I’ll also include the potential values that can be found by using Reflector to look at the Microsoft.SharePoint dll:

  1. ltVar: What kind of storage management information to display
    • List = 1
    • DocumentLibrary = 2
    • Document = 3
  2. sordVar: the direction in which the items are to be sorted
    • Increasing = 0×10
    • Decreasing = 0×11
  3. soVar: whether the items are sorted by size or by date
    • Size=0
    • Date = 1
  4. nMaxResults: the number of results to return

So if you want to find the top 5 largest document libraries in a specific site collection, here’s the PowerShell:

$site = Get-SPSite "http://yoursitecollection:portifneeded";
$dataTable = $site.StorageManagementInformation(2,0x11,0,5);
$dataTable | Select *

This is very helpful if you aren’t yet on SP2010 SP1. A note though – the method is marked as obselete with this description “SPSite.StorageManagementInformation is expensive; avoid using it.”. There’s no further explanation on what is being spooled up to execute the method or why it was OK in SP2007 but not SP2010. So I’d consider this OK to use if you have to…but not in some kind of recurring scheduled script. Your mileage may vary, I’m not responsible for what happens to your farm, etc. etc.

Hope you found this useful. Let me know!

SharePoint 2010: Unable to delete site/web after SP1

While doing some testing after upgrading my dev machine to SP1, I ran into two separate issues when trying to delete a web through the UI by using the “Sites and Workspaces” link in Site Settings. Deleting through powershell worked fine.

Issue #1: Specified method is not supported

This specific error was occurring only on one of my web applications. I had just attached the database for this application to my existing SharePoint 2010 SP1 environment. The error will appear if the database for your web application needs to be updated.

To check if your database needs to be updated, open Central Administration and navigate to “Upgrade and Migration” -> “Review database status”. All of your databases should show a status of “No action required”. If the application you are experiencing the error on is running in compatibility mode, you’ll need to update it using PowerShell. To do this, first get the GUID ID of the database in question by running the following powershell and then copying the ID of the database you wish to upgrade:

Get-SPContentDatabase

Then run the following powershell to upgrade the database using the ID you just copied.

Upgrade-SPContentDatabase -Identity YourDatabaseIDGoesHere

Issue #2: There is no Web named “/YourWebName”

Once my above issue was resolved on the one misbehaving web application, I had consistent behavior across all web applications when trying to delete a web… or so I thought. Turns out – if I create a web right off of the site collection (the RootWeb), then I can successfully delete it using the “Sites and Workspaces” page. Also – I can delete the web successfully if I navigate to the web I want to delete and then use the “Delete this Site” link. This behavior was consistent in a SharePoint 2010 SP1 environment and SharePoint 2010 SP1 + June Cumulative Update. What’s going on here?

Time to open up Reflector. The offending code comes from the Microsoft.SharePoint.ApplicationPages dll located in the virtual directory/_app_bin of your web application. Specifically, the DeleteWebPage class. Here’s the code:

The offending line, using the OpenWeb function, gets passed “/DeleteMe” if your web’s name is “DeleteMe”. This will work fine if the web’s url starting from the site collection is “/DeleteMe”. But if your web is a subweb of a subweb (e.g. http://yoursite/someweb/DeleteMe) then it will fail and throw the exception. This looks to be a bug in SP1 and June CU… so we’ll have to wait for MS to fix the issue before we can go this route to delete a web. Instead – use PowerShell to delete the web or navigate to the web you wish to delete and use the “Delete this Site” link.

SharePoint 2010: Remove existing reusable content items using PowerShell

This same PowerShell could be used for any other list to remove all of the items. I just needed it to remove the OOB reusable content.

$siteColTemp = Get-SPSite "http://yourSiteCollectionUrl";
$reusableContentList = $siteColTemp.RootWeb.Lists["Reusable Content"];
$reusableContentItems = $reusableContentList.Items;
$reusableContentItemsCount = $reusableContentItems.Count;
for($x=$reusableContentItemsCount-1;$x -ge 0; $x--){$reusableContentItems[$x].Delete();}