Saturday, May 24, 2008

SharePoint Database Connection Timeout

So, I'm writing this program that imports content of a medical encyclopedia into a SharePoint publishing site. The content is delivered in the form of about 1500 XML files with each file representing a page in the encyclopedia. Data in each XML file describes how each page is linked the overall structure. (For the most part, it's a tree. Though a few pages have more than one parent.)

Anyway, while the import program works fine, I noticed while running it inside of my very overworked virtual machine, SQL Server periodically goes to "out to lunch" and pegs the CPU to 100% for a few minutes. I guess it's reorg-ing something or another. During that time, the SharePoint API will occasionally throw a "can't connect to the database" exception. Apparently the 45 second default value for timing out connection attempts to the SharePoint database is insufficient. Ouch.

Fortunately, bumping up the timeout value is easy with stsadm. Just invoke the following command to bump it up to (for example) 3 minutes.

stsadm -o setproperty -pn database-connection-timeout -pv 180

Thursday, May 22, 2008

Add Images To SharePoint Publishing Site

Use the C# and the SharePoint API to add images to a publishing site.

using (SPSite siteCollection = new SPSite("http://SomeSiteCollection"))
{
using (SPWeb site = siteCollection.AllWebs["/SomeSite"])
{
SPDocumentLibrary doclib = (SPDocumentLibrary) site.Lists["Images"];

string filename = @"SomeImage.jpg";
string fileDir = @"c:\SomeImageDir";
string doclibRelPath = doclib.RootFolder.ServerRelativeUrl;
string doclibPath = siteCollection.MakeFullUrl(doclibRelPath);
string docPath = doclibPath + '\\' + filename;

Stream documentStream = new FileStream(fileDir + '\\' + filename, FileMode.Open);

SPFile file = site.Files.Add(docPath, documentStream);
file.CheckIn(string.Empty);
file.Publish(string.Empty);
file.Approve(string.Empty);

documentStream.Close();
}
}

Dispose Or Not To Dispose?

The good news is that SharePoint has a full-featured API layer that opens up access to...just about everything within the system.

The bad news is that the API can be a challenge at times due to things like naming inconsistencies between it and the user interface (e.g. SPSite really means "site collection" or instead of PublishingWeb.WelcomePage it's PublishingWeb.DefaultPage) and simply the fact that the surface layer of the API is BIG.

One of the hardest things to get right is figuring out when to call Dispose() on objects returned by the API. If you neglect to invoke Dispose() when needed, serious memory leaks will occur. On the other hand, calling Dispose() when you're not supposed to may (in the words of the Microsoft documentation) "cause the SharePoint object model to behave unpredictably or fail".

How exciting.

Now, there are a large number of blog posts, magazine articles, books, etc out there that help you figure out this Dispose() puzzle. But, this relatively new article article by Roger Lamb is the best that I've seen. It covers not only the "normal" patterns but also some pretty tricky edge cases as well. As a bonus, it also addresses not just core WSS functionality but also other namespaces (e.g. publishing) as well.

Wednesday, May 21, 2008

Exclude SharePoint Folders From Antivirus Scanning

This new knowledge base article caught my eye:

http://support.microsoft.com/kb/952167

MOSS Limits

How many sites can I create? How many documents can SharePoint hold? How many items can I insert into a list?

All this and many more are answered here: http://technet.microsoft.com/en-us/library/cc262787.aspx

SharePoint Webcasts, Podcasts and Labs

This should keep you busy for a long time...

http://www.microsoft.com/events/series/sharepointserver.aspx?tab=overview

Wednesday, May 14, 2008

Accessing SharePoint Publishing Pages With The Object Model

Here's a quick and dirty code sample that "visits" each page in every publishing site within a SharePoint site collection.

In this case, "visiting" consists of writing the check-out and approval status of each page to the console and (if necessary) ensuring that the page is checked-in and approved.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace MyUtilities
{
class Program
{
static void Main(string[] args)
{
const string indent = " ";

using (SPSite site = new SPSite("http://SomeSite"))
{
SPWebCollection webs = site.AllWebs;

foreach (SPWeb web in webs)
{
// only look at publishing sites
if (!PublishingWeb.IsPublishingWeb(web))
{
continue;
}

PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

Console.WriteLine("Site: {0}", pubWeb.Title);

PublishingPageCollection pages = pubWeb.GetPublishingPages();
foreach (PublishingPage page in pages)
{
SPModerationInformation mi = page.ListItem.ModerationInformation;

Console.WriteLine("{0}{1}: Check-out status-{2} Approval status-{3}.",
indent, page.Title,
page.ListItem.File.CheckOutStatus, mi.Status);

try
{
if (page.ListItem.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
page.CheckIn("Automatically checked-in");
Console.WriteLine("{0}*** Checked-in", indent);
}
}
catch (Exception e)
{
Console.WriteLine("{0}Unable to check-in page {1}. Exception: {2}",
indent, page.Title, e.Message);
}

try
{
if (mi.Status != SPModerationStatusType.Approved)
{
page.ListItem.File.Approve("Automatically approved.");
Console.WriteLine("{0}*** Approved", indent);
}
}
catch (Exception e)
{
Console.WriteLine("{0}Unable to approve page {1}. Exception: {2}",
indent, page.Title, e.Message);
}
}
}
}
}
}
}


WSS SDK Object Model Programming Samples

I'm constantly pointing people to these very simple, but helpful SharePoint object model code samples found in the WSS SDK. Problem is, it always takes me a few minutes to locate them. Not anymore...

http://msdn.microsoft.com/en-us/library/ms412748.aspx

Speaking of helpful samples, here's a link to some code that illustrates the following common techniques:
  • getting a reference to a site
  • iterating over all lists in a site
  • getting a reference to a list
  • getting a reference to an item in a list
  • getting a reference to the item's properties
  • getting a reference to a document and its properties
  • adding a new item to a listmodifying an item in a list
http://www.sharepoint-tips.com/2006/08/common-and-simple-coding-tasks-in.html

Tuesday, May 13, 2008

SharePoint Publishing Site Style Sheets

Doug Ware has written a clear and straight-to-the-point explanation of how style sheets render in a SharePoint publishing site.

http://www.elumenotion.com/Blog/Lists/Posts/Post.aspx?ID=48

Monday, May 5, 2008

Checking User Names With Control-K

I was probably the last person on the planet to find this out a few weeks ago, but did you know that within SharePoint, you can use control-k to validate the spelling of any names entered into "people" boxes (e.g. the "Users/Groups" box when adding users to the site)? This shortcut is equivalent to clicking the person-with-a-checkbox icon.