Friday, September 5, 2008

What is Timer Job ?

The SharePoint Timer service is similar to tasks that you can create in any version of Windows by using the Task Scheduler application. The major benefits of using the SharePoint Timer service compared with Windows Task Scheduler jobs is that the timer service knows the topology of the server farm, and you can load balance the jobs across all the servers in the farm or tie them to specific servers that run particular services.

WebPart Custom Properties in ASP.Net

Scenario:
You want to add a custom property for the ASP.Net WebPart

Solution:

private string _sZipCode;
[Personalizable(PersonalizationScope.Shared),
WebBrowsable, WebDisplayName("Select the zip code"),
WebDescription("Use this property to set zipcode")]
public string ZipCode
{
get {return _sZipCode; }
set {_sZipCode = value; }
}

protected override void RenderContents(HtmlTextWriter output)
{
RenderChildren(output);
// Securely write out HTML
output.Write("
Text Property: " + SPEncode.HtmlEncode(Text));
}
Note:
This solution only works if you are inheriting from System.Web.UI.WebControls.WebParts.WebPart.

If you are inheriting Microsoft.SharePoint.WebParts.WebPart class and want to have custom properties, then read the article below.

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

Render vs RenderWebpart vs RenderContents Method

Scenario:
You want to write a basic webpart and want to render some HTML.

Option:
You can either override the Render , RenderWebPart or RenderContents methods to add your own HTML

Recommended:
Use the RenderContents() method.

Reason:
The WebPart base class seals the Render() method of System.Web.UI.Control because the Web Part infrastructure needs to control rendering the contents of a Web Part. For this reason, custom Web Parts must override the RenderWebPart() method of the WebPart base class. But for making your webpart really future proof, suggested way is RenderContents() as future version will be using the same function to render the output.

Sample:

protected override void RenderContents(HtmlTextWriter output)
{
output.Write("Hello World");
}


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

SharePoint Workflow using Visual Studio 2005 with Extensions

Scenario:
You need to write a basic workflow in sharepoint. Once you associate the workflow to a document library , if you upload a document into the library and start the workflow, it will make an entry into TASKS list and another entry into WorkflowTASKS list.

Assumption:
You have Visual Studio 2005 and Extensions for SharePoint installed.

Solution:
01. Open Visual Studio 2005
02. Create new C# Project of type 'Sequential Workflow Library' from SharePoint node
03. Name it 'TestWorkflow'
04. Visual Studio Extension will create some files for you required for deployment.
05. Open 'Feature.xml' and modify it as given in sample below.
06. Open 'Workflow.xml' and modify it as given in sample below.
07. Now open the 'Workflow1.cs'

08. Add a 'CreateTask' activity below 'OnWorkflowActivated'
09. Add a 'LogToHistoryTaskActivity' activity below 'CreateTask'
10. Open 'CreateTask' properties window
11. Click on browse button for taskID > Bind to a new Member > Create Field > Name it 'taskId' > OK
12. Click on browse button for taskProperties > Bind to a new Member > Create Field > Name it 'taskProperties' > OK
13. Double Click on 'CreateTask' and following lines of code

taskId = GUID.NewGUID();
taskProperties.Title = "Task created by workflow" ;
14. Click on 'LogToHistoryTaskActivity' activity and set the
History Description to 'Task Created'
15. Add 'OnTaskChanged' activity
16. Set correlationToken to 'taskToken' and TaskID to 'taskId' , TaskProperties to 'taskProperties'
16. Add a 'CodeActivity'
17. Double click and enter the following code
SPList item = workflowProperties.Item;
SPList task = workflowProperties.TaskList.GetItemById(taskProperties.TaskId);
item.Title ="Task updated by workflow";
item.SystemUpdate(false);

18. Add a 'DeleteTask' activity and set its correlationToken to 'taskToken' and TaskID to 'taskId'

19. Strong Name the project
20. Build the project
21. Run the 'Install.bat'
22. Now this workflow will be available on your local machine for use.

Feature.xml:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="{NewGUIDWithBraces}"
Title="Test Workflow"
Version="1.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="workflow.xml"/>
</ElementManifests>
<Properties>
<Property Key="GloballyAvailable" Value="true" />
</Properties>
</Feature>
Workflow.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
<Workflow Name="My workflow"
Id="NewGUIDWithOUTBraces"
CodeBesideClass="TestWorkflow.Workflow1"
CodeBesideAssembly="TestWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ReplaceITWithKey"
>
</Workflow>
</Elements>
Article:
http://msdn.microsoft.com/en-us/library/ms580283.aspx
http://blogs.msdn.com/nikhil/archive/2008/01/19/sharepoint-workflow-feature-in-vs-2008-part-ii.aspx#8584351

Friday, August 29, 2008

Configuring specific alert template for a single list

Scenario:
You have a special list and you want to use unique email alert template for that list.

Solution:
You need to edit the AlertTemplates.xml file to include the new template which you want to use with a unique name and then you can programatically set this specific alert template for any particular list. Assuming you have created a new alert template in AlertTemplates.xml with the name 'AlertTemplate.SpecialList'.

Code:

using(SPSite site = new SPSite("http://localhost")){
using(SPWeb web = site.Open.RootWeb){
SPList = web.Lists["SpecialList"];

SPAlertTemplateCollection ats = new SPAlertTemplateCollection((SPWebService)(WebApplication.Parent)); 

//give appropriate values for WebApplication
list.AlertTemplate = ats["AlertTemplate.SpecialList"];
list.Update();
}
}
Article:
All about sharepoint alerts , MSDN , Pre Defined Templates

Including / Exculding a module from Site Definition based on Root level or child level web

Scenario:
As of part of definition you want to upload certain files based on weather its a Root Level Web or sub-site.

Solution:
To upload files, documents as a part of site creation , Site definition core file ONET.xml uses Module nodes, which support 'RootWebOnly'. Setting a value of TRUE for the same will result in upload of the files included in the Module only if it a Root Level Web.

Assuming that your Site Defintion name is 'ClientSiteDefinitionName' and you have a folder under that with the name 'WebParts' which contains 'MyCustom.webpart' file.

Snippet from ONET.xml:

<Module Name="CustomWebParts" List="113" Url="_catalogs/wp" SetupPath="SiteTemplates\ClientSiteDefinitionName\WebParts" RootWebOnly="TRUE">
<File Url="MyCustom.webpart" Type="GhostableInLibrary" />
</Module>

Configuring site after site creation

Scenario:
SharePoint doesn't support any event that fires before and after site creation but in many cases you want one of the following:
1. You want to execute some code after site is created.
2. You want to redirect site creator to a custom designed configuration page once site is created.

Solution:
There are various ways to do it but site definition core file ONET.xml support a special node 'ExecuteUrl' which basically takes a valid Url parameter and redirects the site creator to that Url once site is instantiated.

Snippet from ONET.xml:

<ExecuteUrl Url="_layouts/[%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%]/customsettings.aspx"/>