Category Archives: SharePoint

Targeting a SharePoint Site in a Custom Action

In my initial attempt to create a custom action for a SharePoint list, I specified it as follows:

<CustomAction
    Id="SoftArtisans.Tutorial.MenuItemExcel"
    GroupId="ActionsMenu"
    Location="Microsoft.SharePoint.StandardMenu"
    RegistrationType="List"
    Sequence="1000"
    Title="Export to SoftArtisans OfficeWriter for Excel"
    ImageUrl="/_layouts/images/softartisanstutorial/actionicon_excel.gif">
    <UrlAction Url="/_layouts/SoftArtisansTutorial/ExcelWriter.aspx?ListId={ListId}"/>
</CustomAction>

WSS replaces the special {ListId} token with the actual id of the list. In the application page I tried to retrieve the list from which the action had originated as follows:

SPWeb web = SPContext.Current.Web;
string listId = Request.QueryString["ListId"];
SPList list = web.Lists[new Guid(listId)];

However, I found that SPContext.Current.Web always returned the root-level Web site, even if the action was activated from a child site. So I modified the URL of the action to include the URL of the site: Continue reading Targeting a SharePoint Site in a Custom Action

Adding a View When Provisioning a Custom List

When creating a custom list, you need to provide a list definition in the form of a schema.xml file. Rather than creating a schema.xml file from scratch, a recommended approach is to copy an existing list definition. The out-of-the-box SharePoint custom list is a good starting point as it provides the barest minimum of features required. Armed with a stock list definition, you need to modify it for the list you want to create. If you want to use a specific content type, you can specify it in the <ContentTypes> section. You need to specify the columns of the list in the <Fields> section. Finally, you will want to modify the view definition to specify the columns to be included in the view.

Modifying an existing list definition is a quick way to create a custom list. Often, however, you will want to have more control over the view definition other than what fields are included in it. The view definition is the largest section in the schema.xml and can be rather daunting. Here is where SharePoint Manager comes in. Continue reading Adding a View When Provisioning a Custom List

Defining Managed Path When Creating New SharePoint Site

I needed to create a new sub-site underneath an existing SharePoint site, which is located at http://server/site1. The sub-site’s URL should be http://server/site1/site2. The simplest way to create a new site is to use an option normally exposed through the UI, such as the Create page under the Site Actions menu. However, in this case, I’m working with an existing site that’s customized so that the Site Actions menu isn’t available.

The first idea that occurred to me was to use Central Adminitration to add a new site collection. To do so, I selected Create Site Collection under the Application Management menu. In order to create the new site collection under the desired path, I decided to define site1 as a managed path. This worked well enough and allowed me to create a new site collection under http://server/site1/site2. However, to my horror I discovered that the site located at http://server/site1 had disappeared, replaced by the dreaded HTTP 404 screen! Continue reading Defining Managed Path When Creating New SharePoint Site

Amazingly Simple (and Fast) XML Formatting Trick

While pre-populating a SharePoint list instance with data, I find myself having to insert a large amount of unformatted data into an XML file in Visual Studio 2008. When formatted, the XML data consist of roughly 12000 lines. After I pasted the data into the file, Visual Studio took several agonizing minutes to format them into nicely human-readable form. Moreover, during this time, my computer’s CPU usage was also pegged at or near the maximum, rendering it practically unusable.

However, I found that if the XML data are already mostly formatted, then Visual Studio leaves them alone. So I tried to load the XML data into a text editor to see whether it could do the job faster. Unfortunately, my text editor isn’t capable of automatically formatting XML, and using regular expressions is too manual of a process.

Finally, I noticed that Internet Explorer loads and formats an XML file almost instantaneously. This suggests the simple trick of opening the XML file in IE, pasting the formatted XML content into a text editor to remove the extraneous hyphens, then pasting the results into Visual Studio. Doing so reduces the amount of time for the process from several minutes to just a few seconds.

Searching for Files with CAML

I was interested in searching for a file in a Document Library. Using a CAML query is much more efficient than iterating through the Document Library and examining each file. There are a couple of scenarios.

Searching at the root level of the Document Library

Searching for files is a little different from searching for other list items. In SharePoint, a file is shown with the same attributes as any other list item. When creating the CAML query, it would seem natural to search a field such as Title for a given file name. However, for a document, the Title field by default doesn’t store the file name. Rather, it contains arbitrary content that can be edited in Word (under the document’s Properties).

Furthermore, when creating the CAML query, you must reference a field using its internal name. So you won’t be able to use a field such as “Name”, as it is not a valid internal name (source: List of internal names for SharePoint fields).

Incorrect internal name error
If you don’t use the correct internal name of a field in a query, you may get the following error when attempting to access the results of the query: One or more field types are not installed properly. Go to the list settings page to delete these fields.

The correct field to use when searching for files is FileLeafRef, which is the appropriate internal name to use for the Name field. The C# snippet to create the query is as follows: Continue reading Searching for Files with CAML

List of Internal Names for SharePoint Fields

In order to reference a column or field using the the SharePoint object model, you often need to know its internal name. For example, when creating a CAML query, you can specify the field on which to search by providing its internal name. As opposed to the display name, which can be changed in the UI, the internal name is immutable. While the default display name and the internal name are often identical or similar, they can also be very different. The difference isn’t always consistent. For example, spaces in the display name can be removed, such as IsCheckedoutToLocal, or replaced with the hexadecimal equivalent, such as HTML_x0020_File_x0020_Type. Furthermore, the display name can be the same for more than one fields, so the internal name is the only way to distinguish between them.

You can quickly determine the internal name of a field using the UI:

  1. Open the List Settings page
  2. Under the Columns section, select a column to view the Edit Column page
  3. The URL of this page includes the internal name in the query string. For example, the URL for the Created By field includes the following query string List=%7BF641CEF1%2DCDE2%2D49E1%2D9800%2D861A408EF890%7D&Field=Author. The value for the Field parameter, Author, is the internal name for Created By. Continue reading List of Internal Names for SharePoint Fields

Missing Filter Web Parts in SharePoint

While creating a web part page I discovered that the Query String Filter web part was missing. In fact, none of the filter web parts was available. A search for “filter” revealed that the filter web parts belong to a site-level feature called BizAppsSiteTemplates. However, for some reason, this feature wasn’t activated. Furthermore, it had the Hidden attribute turned on so I couldn’t see it in the site feature list. I tried to manually install the feature using STSADM, but it turned out the feature was alread installed. So I activated it with STSADM:

stsadm -o activatefeature -name bizappssitetemplates -url http://moss2007/mysite

After that the filter web parts finally showed up in the Web Part Gallery.

Once you locate the DWP file for a web part, you can also upload it directly to the Web Part Gallery. But activating the parent feature is more efficient and elegant.