Category Archives: MOSS 2007

Clean Up Customized CSS Files with CSSCompare

Have you ever come across a SharePoint installation that’s been customized in an unsupported way? You look at the 12 (or 14) hive and see that files have been modified by hand? Not good.

As you may know, changing core SharePoint files is unsupported and can lead to problems when patching.

The best way to clean up modified SharePoint installations is to pull out the customizations and move them into new files as part of a solution package (WSP file). That makes it easy to re-deploy customizations and ensures your environment will stay clean.

Read more here and download CSSCompare here.

Christmas Time – Snow in Home Page

Its Christmas time and time for some fun with our SharePoint site. Here is a quick trick to show snow fall in your SharePoint page (May be your Intranet Home Page). It will take you just few minutes to setup.

1. Go to this site and download Snowstorm.js file and upload it to some Document Library

2. Open your SharePoint page and add a Content Editor WebPart.

3. Edit the HTML Source for the content Editor WebPart

4. Add the following code and set the Chrome Type for the Web Part as none

[sourcecode language="javascript"]
<script src="{Path}/snowstorm-min.js"></script>
[/sourcecode]

5. Save and Exit Edit mode and that’s it! :-)

It’s always a good idea to have a “Stop Snowing” link for those who find this annoying. Simply edit the JavaScript code in the Content Editor WebPart and add the following code.

[sourcecode language="javascript"]
<script type="text/javascript" src="/Docs/snowstorm-min.js"></script>
<a onClick="javascript:snowStorm.stop();" href="#">Stop Snowing</a>
[/sourcecode]

There are many other customization available. Check this page for more options.

SharePoint Workflow history list never gets deleted

This post will help to understand the different issues and solutions with workflow history stored in MOSS 2007 and SharePoint 2010.

There are organizations were numerous workflows are created that are developed using, either out of the box features or SharePoint Designer or Visual Studio developed or even third party tools like Nintex or K2.

What is a workflow ? - A workflow is comprised of a sequence of events such as workflow initiation, task creation, and task completion. When you add a workflow, you specify a task list and history list that workflow instances of that workflow association will use to track the events for the workflow. The task list contains the items that users interact with and enter data into while interacting with the workflow. The workflow history contains key information about each event including date, status, participant, and description.

Workflow history list – This is a hidden list in all SharePoint sites and this could be accessed by appending /Lists/Workflow History/ to the address of the SharePoint site. Many of the organisations would like to keep these lists as long as possible to maintain as a backup for the workflows created. But what about the bulk amount of database space that it occupies?

Workflow Auto Cleanup timer job – The Workflow history list is deleted by default using the Workflow Auto Cleanup job. This job should delete all workflow instances and related task entries that exist 60 days after the workflow is completed or cancelled. Thank God!!! All those unwanted entries could be deleted automatically by running the timer job. If you wish to keep the workflow history list, it could be done by disabling the timer job. Go to Central Admin -> Operations Tab (SharePoint 2007) -> Timer job definitions -> Find the timer job, click open and just disable.

There are also patches available from Microsoft technical support to increase the number of days from 60 to a larger number, thus increasing the amount of time that the workflow histories could be retained - http://blogs.msdn.com/b/nishand/archive/2007/09/10/changing-the-spworkflowassociation-autocleanupdays-property.aspx

Workflow history list never gets deleted

Wonder why the database space is still getting eaten up even though the workflow history list are scheduled for auto cleanup. Recently there was an msdn post which described that even though the timer job is enabled, the Workflow history lists are infact not getting deleted. As the post explains, Workflow history items themselves are not deleted, but the entry point to view them on the status page for a particular instance will no longer be available.

The post has a code sample that could be used to actually delete all the Workflow history list items. Those who would be running workflows on K2 or Nintex could also check with the vendors, how these lists are managed by them.

Complex CAML queries made simple

Update: If you want a quicker solution, please check out my new CAML Viewer Web Part for SharePoint 2010.

In this post we will see how we can generate complex CAML queries easily. We will also see how we can use this query to fetch data from the list.

If you want to create a complex query, first create a list view based on the required criteria. Then use the below code to get the query.

[sourcecode language="csharp"]
SPWeb web = SPControl.GetContextWeb(this.Context);
SPList spList = web.Lists["Mylist"];
SPView view = spList.Views["Myview"];
Response.Write(view.Query.ToString());
[/sourcecode]

Once we have this CAML query we can easily extract data from SharePoint list. Below is the code to fetch data from SharePoint list using CAML query

[sourcecode language="csharp"]
SPQuery query = new SPQuery();
query.Query = view.Query.ToString();
SPListItemCollection cole = spList.GetItems(query);
foreach (SPListItem item in cole)
{
Response.Write(item["Title"] +"<br />");
}
[/sourcecode]

Creating SharePoint list and add column programmatically

In this post we will see two methods for creating SharePoint list programmatically and add column to the created list

Using Object model
[sourcecode language="csharp"]SPSite site = new SPSite(SPContext.Current.Site.ID);
SPWeb web = site.OpenWeb();
SPListTemplate template = web.ListTemplates["Custom List"];
web.AllowUnsafeUpdates = true;
web.Lists.Add("MyList", "This list created using code", template);
SPList mylist = web.Lists["MyList"];
mylist.Fields.Add("MyField", SPFieldType.Text, false);
mylist.Update();
web.AllowUnsafeUpdates = false;[/sourcecode]

Using PowerShell Script

[sourcecode language="powershell"]
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://mysite");
$OpenWeb = $SpSite.OpenWeb();
$TemplateType = $OpenWeb.ListTemplates["Custom List"];
$OpenWeb.Lists.Add("my list"," ",$TemplateType);
$list=$Openweb.Lists["my list"]
$list.Fields.Add("My field", "Text", 0)
$OpenWeb.Dispose();
$SPSite.Dispose();[/sourcecode]

How to Add calculated column in SharePoint list programmatically

In this post we will see two methods for adding calculated field in SharePoint list.
Using Object model

[sourcecode language="csharp"]
SPSite site = new SPSite(SPContext.Current.Site.ID);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList mylist = web.Lists["MyList"];
mylist.Fields.Add("CalcField", SPFieldType.Calculated, false);
SPFieldCalculated CalcField = (SPFieldCalculated)mylist.Fields["CalcField"];
CalcField.Formula = @"=CONCATENATE("" Calc : "",[Title])";
CalcField.Update();
mylist.Update();
web.AllowUnsafeUpdates = false;[/sourcecode]

Using PowerShell Script

[sourcecode language="powershell"]
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://mysite");
$OpenWeb = $SpSite.OpenWeb();
$list=$Openweb.Lists["MyList"]
$list.Fields.Add("CalcField", "Calculated", 0)
$SPField = $list.Fields.GetField("CalcField")
$SPField.Formula="=CONCATENATE([Field1],"" : "",[Field2])"
$SPField.Update()
$OpenWeb.Dispose();
$SPSite.Dispose();
[/sourcecode]

Object reference not set to an instance of an object – stsadm addsolution

SharePoint is know to throw completely meaningless errors and this is one of them. Have you ever tried adding a solution using stsadm and received the following error?

Object reference not set to an instance of an object

Don’t panic! there is nothing wrong with your code or wsp. Just make sure the user account you use to run the stsadm command is a Farm Administrator account.

Simple Custom Feature in SharePoint

In this article we will see how we can develop a simple future for SharePoint.

Before starting the development, make sure that the following tools are installed.
1. Visual studio extension for windows SharePoint services:
http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=7bf65b28-06e2-4e87-9bad-086e32185e68&displayLang=en

Note: You can refer the link
http://www.allaboutmoss.com/index.php/2010/03/22/install-visual-studio-2005-extensions-in-windows-xp-or-vista/ for installing Visual studio extension in Windows XP

2. WSP Builder: http://www.codeplex.com/wspbuilder

Let’s get started. Fire up visual studio

Click File -> New ->  Project

Choose the language (I am choosing c# for this tutorial) and select WSP Builder Project . Enter a project name and click OK.

Add the reference for sharepoint DLL in your project.

Right click the project click on property and choose signing create a strong key and click OK without password. Then save the project.

Add feature template in this project by Right clicking the project -> Add -> New Item
Choose WSPbuilder, select Feature with receiver and give the name of  feature. Click OK

Then enter the feature settings (I am choosing web as scope)

Scope specifies the scope in which the Feature can be activated and contains one of the following values: Farm (farm), WebApplication (Web application), Site (site collection), Web (Web site).

Open the simplefeature.cs file

We have to comment below code from the FeatureActivated,FeatureDeactivating, FeatureInstalled and FeatureUninstalling method.

[sourcecode language="csharp"]
throw new Exception(“The method or operation is not implemented.”);[/sourcecode]

Add the following code in FeatureActivated method. This code is to create a Custom List in the site while activating feature.

[sourcecode language="csharp"]
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPListTemplateCollection customListTemplates = site.GetCustomListTemplates(web);
SPListTemplate listTemplate = web.ListTemplates["Custom List"];
web.Lists.Add(“Mylist”, “A custom list created by feature”, listTemplate);
web.Update();
web.AllowUnsafeUpdates = false;[/sourcecode]

That’s it. Build your project and make sure there are no errors.
Next step is to build WSP for this.Right click the Solution -> WSP Builder -> Build WSP.

Now you will see a new file named simplefeature.wsp in your project folder.

Deploy and Activate this feature in server

  • Copy the WSP to your SharePoint server
  • Open command prompt and navigate to the folder Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
  • run  stsadm -o addsolution -filename “G:\simplefeature.wsp”
  • Open central administration -> click operations
  • Click Solutions under Global configurations
  • Locate the newly added web part (simplefeature.wsp) and click to open its solution property page.
  • Click solution deploy link and click OK in the resulting page.
  • Browse our sharepoint site
  • Go to Site Action-> Site settings -> Site feature
  • Activate Simplefeature

Now you can see a custom list named ‘Mylist’ is created in your site.

SharePoint Training in Trivandrum

We are glad to Announce that we are starting SharePoint training in Trivandrum, India. If you are in or around Trivandrum and would like to learn SharePoint from Experts, contact us for details.

Classes are normally conducted on weekends so that even working professionals can start learning without affecting their work schedule.

Contact us for Course Details and Introductory offers from allaboutmoss. Don’t forget to spread this word ;-)

Note: We also provide on demand SharePoint training if you are anywhere in India.

Setting a custom 404 error page for SharePoint

In this article we will see how to set a custom error 404 (page not found) error page for SharePoint.

  1. Log on the sharepoint server and access the language folder in layout of IIS (% systemdrive%\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\LangID)
    Note: In this path, LangID represents the actual Locale ID of the language that you use. For example, 1033 is the language ID for U.S. English.
  2. Rename the error.html file to error.old.
  3. Create the custom HTML file (pagenotfound.html) and copy it to above folder.
  4. Create a console
  5. application / PowerShell script for assigning the new custom error page for your web application and run / execute it in SharePoint server

C# code for console application

[sourcecode language="csharp"]
Microsoft.SharePoint.Administration.SPWebApplication webapp =
Microsoft.SharePoint.Administration.SPWebApplication.Lookup(new Uri("http://"));
webapp.FileNotFoundPage = "pagenotfound.html";
webapp.Update();
[/sourcecode]

PowerShell code

[sourcecode language="powershell"]
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://")
$webapp.FileNotFoundPage = "pagenotfound.html"
$webapp.Update()
[/sourcecode]

Note: By default, in Internet Explorer, the Show friendly HTTP error messages setting is turned on and the custom error page may not appear. In this case turn off this setting and try.