Thursday, April 28, 2011

Deleting a SharePoint List that errors with Invalid Filename

Ever get in a situation where you create an instance of a list from a list definition, then remove that list definition, and then you're hard pressed to delete it: You can't via the UI, Manage Content and Structure throws an error, and it doesn't show in SharePoint Designer?

Well you can delete it using using PowerShell with an assist from SharePoint Manager:

1) Open SharePoint Manager, go to the list you want to delete, and copy the ID of it.
2) In PowerShell, run the following:

$web = Get-SPWeb "http://dev.yoursite.com"
$lc = $web.Lists
$lc.Delete("YOURGUID")

and then it'll be removed.

Tuesday, April 26, 2011

Adding JavaScript into RichHtmlField on Publishing Sites

To try and prevent scripting attacks, the RichHtmlField will remove any JavaScript code out when saving/checking in/publishing a page, and sadly there's no option to make it allow script tags. To get around this, I've used two alternatives in the past:

1) Add a Content Editor Web Part to your page, which will not strip JavaScript
2) Use the Telerik RadEditor

Any other alternatives are welcome!

"Save as Site Template" functionality for Publishing Sites via PowerShell

I came across a situation where I wanted to create a whole bunch of publishing sites, using one site as a template. Knowing that OOB, the "Save as Site Template" functionality is disabled (well kinda, you can always go straight to the url: /_layouts/savetmpl.aspx and give that a shot), I came up with an alternative using PowerShell.

What it's basically doing is exporting a site, creating a new site and then importing over the new site. The code is below:

# This creates sites based on the template provided.

function CreateWeb([string]$path, [string]$url, [string]$name)
{
Write-Host "Url: $url"
Write-Host "Path: $path"
Write-Host "Name: $name"

# Import the web that we saved
New-SPWeb -Url $url -Template "CMSPUBLISHING#0" -Name $name
Import-SPWeb -Identity $url -Path $path
$web = Get-SPWeb -Identity $url
$web.Title = $name
$web.Update()

# Change the Title of the page and publish it.
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))
{
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pubWeb.PagesList
foreach($item in $pages.Items)
{
$pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)

$pubPage.CheckOut()
$pubPage.Title = $name
$pubPage.Update();
$pubPage.CheckIn("")
$pageFile = $pubPage.ListItem.File;
$pageFile.Publish("");
#$pageFile.Approve("");

}
}

$web.Dispose()

}

$path = "C:\Development\yourexportedweb.cmp"

# Create webs
CreateWeb $path "http://server/industries/financial" "Financial Services"
CreateWeb $path "http://server/industries/healthcare" "Healthcare"
CreateWeb $path "http://server/industries/insurance" "Insurance"

This is great for developers and admins, but not so much for end users, which is where the real need is. That'll have to be the next step...

Wednesday, April 20, 2011

Web Analytics Reports error with Custom Master Page

If you're using a custom master page and get an exception when trying to access the Web Analytics Reports that looks like this:
System.ArgumentException: Could not find the sitemap node with URL '/_layouts/WebAnalytics/WebAppSelection.aspx'. 
 The error is getting thrown from the PlaceHolderHorizontalNav, and if you clear it's contents inside you'll resolve the error:

              <asp:ContentPlaceHolder id="PlaceHolderHorizontalNav" runat="server">
                    <!-- Remove this to fix Web Analytics Report exception
                    <SharePoint:AspMenu
                          ID="TopNavigationMenuV4"
                          Runat="server"
                          EnableViewState="false"
                          DataSourceID="topSiteMap"
                          AccessKey="<%$Resources:wss,navigation_accesskey%>"
                          UseSimpleRendering="true"
                          UseSeparateCss="false"
                          Orientation="Horizontal"
                          StaticDisplayLevels="2"
                          MaximumDynamicDisplayLevels="1"
                          SkipLinkText=""
                          CssClass="s4-tn"/>
                        <SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource" Id="topNavigationDelegate">
                            <Template_Controls>
                                <asp:SiteMapDataSource
                                  ShowStartingNode="False"
                                  SiteMapProvider="SPNavigationProvider"
                                  id="topSiteMap"
                                  runat="server"
                                  StartingNodeUrl="sid:1002"/>
                            </Template_Controls>
                        </SharePoint:DelegateControl>
                       -->
                </asp:ContentPlaceHolder><sharepoint:aspmenu >

Sunday, April 3, 2011

Programatically Setting Column Default Value for Managed Metadata Column

I've been working on setting up document libraries with different column default values for managed metadata based on what site the library is in.  I achieved this with the following:  
  
        using (SPSite siteCollection = new SPSite("http://www.yoursite.com"))
        {
            using (SPWeb web = siteCollection.OpenWeb("/yourweb/"))
            {
                SPList customDocumentLibrary = web.Lists["Documents"];
                SPFolder rootFolder = customDocumentLibrary.RootFolder;

                MetadataDefaults columnDefaults = new MetadataDefaults(customDocumentLibrary);

                columnDefaults.RemoveAllDefaults();
                columnDefaults.SetFieldDefault(rootFolder, "Programs", "1033;#Academics|bc943091-79ac-4f5f-a79b-205e8e717823");
                columnDefaults.Update();
            }
        }

Note that if you don't add the proper value in the correct format you'll get the following error message:
 
The given value for a taxonomy field was not formatted in the required <int>;#
 
Where int is the lcid, label is the Term Name, and the Guid is the Term Guid.