Creating a custom Hello World web service in SharePoint

We need to do the following five steps

1) Create the web service.

2) Run disco.exe to get static disco and wsdl file for the web service.

3) Put asmx, disco and wsdl file to ISAPI folder.

4) Rename disco and wsdl file to aspx. Replace static url’s in them.

5) Add an entry to spdisco.aspx

Let’s proceed

· Create a new ASP.NET web service project.

· Rename Service.asmx to HWService.asmx.

· Right click it – select View Mark Up.

· And put the following mark up. Here we will be creating the web service with inline code.

<%@ WebService Language=”C#” Class=”MyInlineService.HelloWorldService” %>

using System.Web.Services;

namespace MyInlineService


public class HelloWorldService: WebService



public string HelloWorld()


return “Hello World”;




· Now Run Disco.exe for our HWService.asmx to create static disco and wsdl file.

· Open visual studio command prompt and run the following command

disco http://localhost:1814/HelloWorldSPService/HWService.asmx

· This will create HWService.wsdl and HWService.disco files at the current directory.

· Now copy all the three files HWService.asmx,HWService.wsdl and HWService.disco to ISAPI folder.

· Rename HWService.disco to HWServicedisco.aspx and HWService.wsdl to HWServicewsdl.aspx.

· Now we will open these new renamed wsdl and disco files to make the following modifications.

Open HWServicewsdl.aspx.


<?xml version=”1.0″ encoding=”utf-8″?>


<%@ Page Language=”C#” Inherits=”System.Web.UI.Page” %>

<%@ Assembly Name=”Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Import Namespace=”Microsoft.SharePoint.Utilities” %>

<%@ Import Namespace=”Microsoft.SharePoint” %>

<% Response.ContentType = “text/xml”; %>


<soap:address location=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; />


<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

Open HWServicedisco.aspx


<?xml version=”1.0″ encoding=”utf-8″?>


<%@ Page Language=”C#” Inherits=”System.Web.UI.Page” %>

<%@ Assembly Name=”Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Import Namespace=”Microsoft.SharePoint.Utilities” %>

<%@ Import Namespace=”Microsoft.SharePoint” %>

<% Response.ContentType = “text/xml”; %>


<contractRef ref=”http://localhost:1814/HelloWorldSPService/HWService.asmx?wsdl&#8221; docRef=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns=”; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q1=”; binding=”q1:HelloWorldServiceSoap” xmlns=”; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q2=”; binding=”q2:HelloWorldServiceSoap12″ xmlns=”; />


<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + “?wsdl”),Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns=”; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1=”; binding=”q1:HelloWorld” xmlns=”; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2=”; binding=”q2:ServiceSoap12″ xmlns=”; />

· Adding it’s entry in spdisco.aspx

Open the spdisco.aspx and add the following entry

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?wsdl”), Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx”), Response.Output); %>

xmlns=” />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?disco”),Response.Output); %>

xmlns=”; />

Now your web service should be availabe to be used within sharepoint context


That’s it!!

Create a simple hello world custom Application Page in SharePoint.

There are two approaches we can follow while creating an Application Page in SharePoint.

One is Inline Approach

·         Create a new default.aspx page rename it to HelloWorldAppPage.aspx

·         Put the following markup in it

<%@ Page Language=”C#” MasterPageFile=”~/_layouts/application.master” Inherits=”Microsoft.SharePoint.WebControls.LayoutsPageBase” %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

  <asp:Label Text=”Hello World” runat=”server” ></asp:Label>


·         Every application page is an content page in SharePoint. And here to have same look and feel as the other application pages we have specified the application.master as the masterpagefile.

·         Inherits attribute of the Page directive refers to LayoutsPageBase class from which the application pages inherit.

·         We have than used a content server control that would be referring to the PlaceHolderMain content place holder inside the application.master page.

·         Than we have simple added a label control that would be displaying hello world.

·         Now go to your 12 hive i.e. C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS and create a folder (MyHelloWorld) over there and put the HelloWorldAppPage.aspx over there.

·         We could have directly placed the aspx page to layouts folder but than from better organization perspective we have created a folder and kept the aspx page inside it.

·         Now the page is available to all the SharePoint sites.


Another is code behind approach.

·         The easiest way is to first create a Asp.NET Web site project.

·         Add a class library project to the solution (GCL)

·         Delete class1.cs and copy the default.aspx.cs page from web site project to the class library project.

·         Delete the default.aspx.cs from the website.

·         Add reference to System.Web and Microsoft.SharePoint.dll  in your class library project

·         Put the following code in the _Default

public partial class _Default : LayoutsPageBase


    protected Label Label1;

    protected void Page_Load(object sender, EventArgs e)


        Label1.Text = “Hello World Code Behind”;




·         Sign the assembly and put it in GAC.

·         Get the public key token.

·         In the default.aspx page replace and put the following markup to use the assembly.

<%@ Page Language=”C#” MasterPageFile=”~/_layouts/application.master”

 Inherits=”_Default,GCL, Version=,Culture=neutral,PublicKeyToken=16391a8a7c882343″ %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

  <asp:Label ID=”Label1″ Text=”Hello World” runat=”server” ></asp:Label>


·         Put default.aspx to the  layouts folder or your custom folder inside it.

That’ s it !!

Creating a Custom Web Service in SharePoint for Uploading Document

We had a requirement to upload the document to SharePoint document library whenever a file has been attached to our entities in Microsoft Dynamics CRM. So the obvious way of doing that was to use web service within the CRM. We had already written a utility using SharePoint Object Model that would be doing so. So we thought of reusing the existing logic defined over there and wrap it inside one our custom web service’s method. We created the web service and we deployed it in our SharePoint Server but outside the context of the SharePoint i.e. as a separate web site. However when trying to use that web service from other machines we ran into security issues, it was always giving Unauthorized Access error. We tried running the web service app pool with identity of administrator and also tried out impersonation defined through web.config again passing the account of administrator. But it didn’t work

Then finally tried out this article within MSDN which guides us on creating a web service that operates within the context of WSS 3.0.

Basic steps for creating a custom web service are following

1) Create a new web service project.

2) Add a new class library project in that solution.

3) Move Service1.cs file from web service project to class library project

4) Delete the Service1.cs file in web service project and class1.cs file in the class library project.

5) Put your custom logic in service.cs file that is now in the class library project.

6) Give strong name to the class library i.e. Sign the assembly.

7) Put the assembly in GAC. Get its public key token.

8) Now make modification to the service.asmx to use the assembly.

9) Replace code behind attribute with the class attribute to refer to the assembly

<%@ WebService Language=“C#” Class=“Service, MyServiceAssembly, Version=, Culture=neutral, PublicKeyToken=8f2dca3c0f2d0131” %>

10) Generate static discovery and wsdl file.

11) Copy the .asmx file of the Web service to \Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS

12) From visual studio command prompt run the following command

disco http://MyServer:port/_layouts/MyCustomWebService.asmx

13) open both the .disco and .wsdl files and replace the opening XML processing instruction — <?xml version="1.0" encoding="utf-8"?> — with instructions such as the following

<%@ Page Language=“C#” Inherits=“System.Web.UI.Page” %>

<%@ Assembly Name=“Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Import Namespace=“Microsoft.SharePoint.Utilities” %>

<%@ Import Namespace=“Microsoft.SharePoint” %>

<% Response.ContentType = “text/xml”; %>

14) In the .disco file, modify the contract reference and SOAP address tags

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + “?wsdl”),Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns=; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1=; binding=“q1:HelloWorld” xmlns=; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2=; binding=“q2:ServiceSoap12” xmlns=; />

15) In the .wsdl file, make the following changes for the SOAP address

<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

16) Rename both files in the respective formats MyCustomWebServicedisco.aspx and MyCustomWebServicewsdl.aspx so that your service is discoverable through Windows SharePoint Services.

17) Copy the new MyCustomWebServicewsdl.aspx and MyCustomWebServicedisco.aspx files, and also the MyCustomWebService.asmx fil to _Drive:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI directory

18) Open the spdisco.aspx files located in Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI and add the following code, specifying the .asmx file for your Web service.

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx?wsdl), Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx), Response.Output); %>

xmlns= />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx?disco),Response.Output); %>

xmlns=; />

Any modification done to the custom logic would require putting the new assembly in GAC followed by generating the static disco and wsdl file and making all the necessary modification in disco and wsdl file as mentioned in the above steps.

Now in our case finally putting the custom logic of uploading the document within the class library for the web service helped us to solve the issues that we were facing.


public string UploadDocumentToSharePoint(string fileName, byte[] fileContents, string folderName)


if (fileContents == null)


return “Null Attachment”;




SPWeb web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

SPFolder folder = web.GetFolder(folderName);

string fileURL = fileName;

folder.Files.Add(fileURL, fileContents);

return “File added successfully!”;


catch (System.Exception ex)


return “Error: “ + ex.Source + “| Message: “ + ex.Message;



And this is how we used it in within our custom application after adding the web reference to it.

FileStream oFileStream = File.OpenRead(@”C:g.doc”);

byte[] content = new byte[oFileStream.Length];

// store the contents in a byte array

oFileStream.Read(content, 0, (int)oFileStream.Length);

// close the stream


// instantiate the service

DocService.Service docService = new DocService.Service();

// pass the current logged in user credentials

docService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

// pass the name of the doc, content and document library name

string message = docService.UploadDocumentToSharePoint(“G1.doc”, content, “CrmDoc”);



Deploying a web part in SharePoint.

We need to do the following to deploy the web part.

  • First we need to sign the assembly.
  • In Visual Studio right click project and select Properties.
  • In properties, select Signing Tab.
  • Select Sign the assembly check box.
  • And specify the file or create a new file.
  • Build the assembly.
  • Now the manual way of deploying the assembly would be to put it either in GAC or bin directory of the web application and put a safe control entry within the web.config of the web application.
  • However the preferred way of deploying is through a solution file, which would make our web part available in the solution management ( global configuration section) of Central administration page, from where we can deploy to multiple web applications.
  • For this we need to create a manifest file.
  • Right click the project.
  • Add New Item – Select an xml file – name it manifest.xml
  • Put the following code on the file

<?xml version=1.0 encoding=utf-8 ?>

<Solution xmlns=



                                <Assembly DeploymentTarget=WebApplication




                                                                Assembly=DeploymentWebPart, Version=, Culture=neutral,


                                                                Namespace=DeploymentWebPart TypeName=*/>







Soution ID – Create and assign a new guid.

DeploymentTarget attribute, which has two possible values: GlobalAssemblyCache or WebApplication.

 GlobalAssemblyCache indicates that the assembly should be deployed in the global assembly cache; WebApplication tells Windows SharePoint Services to drop the assembly in the private application folder of the IIS Web application

SafeControl element describes the configuration that must be done in the web.config file.

  • Now we will create the solution package, which is essentially a cabinet file that would contain our assembly and manifest file.
  • For this right click the project and select a CAB project from SetUp and Deployment projects folder.
  • Name it as DeploymentWebPartSolution
  • Right click the CAB Project and select – Add – Project Output.
  • In Project Output Group dialog box select the DeploymentWebPart and from the configuration drop down select Active.
  • In the project list box, select Primary Output to include the assembly.
  • Now again right click the cab project and select – Add – Project Output, this time select Content Files to include the manifest.xml.
  • Build the Project.
  • This will create DeploymentWebPartSolution.CAB file.
  • Rename it to DeploymentWebPartSolution.wsp
  • To add the solution package to solution store
  • Stsadm.exe -o addsolution -filename DeploymentWebPartSolution.wsp.
  • We now go to Solution Management link inside the Global Configuration section within Operations Tab of Central Administration Site.

There we will see our DeploymentWebPartSolution.wsp. Now click on it and select the web application where it should be deployed.

However if are creating our web part using web part project template within Visual studio 2008,  .wsp solution package automatically gets created for us when we select deploy command on right clicking the project. We can than make use of the .wsp file directly, no need to create a setup project in that case.


That’s it ….


Creating a Hello World Connectable Web Parts in SharePoint.

To write connectable web parts we need to do the following

First we need to define our own interface that will specify the data we want to pass from one web part to another.

The provider web part needs to do the following

  • Implement the interface.
  • Create a property which would be returning a reference to the interface.
  • The property should be decorated with ConnectionProvider attribute.

The consumer web part needs to do the following

  • It should contain the method which would receive the interface.
  • The method should be decorated with ConnectionConsumer attribute.

Keeping the above information in mind let’s start

 Create a new web part project within Visual Studio 2008. (HelloWorldConnectedWebPart)

Right click on the Project

Select Add New ItemàSharePointàWebPart.

Now rename the webpart1.cs and webpart2.cs class as  HWProviderWebPart and HWConsumerWebPart respectively.

Now right click the project and add a new interface class.

    public interface IStringData


        string ProviderStringInfo { get; }


Our Provider class should implement this interface and define one property which would be returning the reference to the interface.


    public class HWProviderWebPart : WebPart, IStringData


        public HWProviderWebPart()



        protected override void CreateChildControls()



        // the string info that would be passed to the consumer

        protected string myInfo = “Hello World”;    

        // implement the property defined in the interface

        public string ProviderStringInfo


            get { return myInfo; }


        // create a property which would be returning the interface reference

        // decorate it with ConnectionProvider

        [ConnectionProvider(“String Provider”)]

        public IStringData ConnectionInterface()


            return this;




Now let’s move to our Consumer Web Part.


    public class HWConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart


        public HWConsumerWebPart(){


        protected override void CreateChildControls(){      



        IStringData myProviderInterface = null;

        // The Consumer class should define a method that would accept

        // the interface as an parameter

        // Should be decorated with ConnectionConsumer attribute

 [ConnectionConsumer(“String Provider”)]

        public void GetInterface(IStringData providerInterface)


            myProviderInterface = providerInterface;



        protected override void Render(HtmlTextWriter writer)




                // priting the value provided by provider web part



            catch(Exception ex)


                writer.Write(“Error info “ + ex.Message);





Now build the project. ( Remove errors if any)

Right click the project.

Select Properties – Debug — Start browser with url ( Specify the site where the web part should be deployed)

Right click the project and select Deploy.

After Deploy Succeeds ,

Go to site actions — Site Settings — WebParts( Inside galleries) –Click on New– Select both the Provider and consumer web part — Populate Gallery.

Go to your home page —  Edit page — Add both the web parts –Select the provider web part — Connections and Specify the connection.

That’s it..

Understanding Web Part life cycle

Web Part Life Cycle starts with

OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part.

LoadViewState – The view state of the web part is populated over here.

CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() – It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.


User Generated Event – for e.g. button click on the web part.

OnPreRenderHere we can change any of the web part properties before the control output is


RenderContents – Html Output is generated.

SaveViewState – View state of the web part is serialized and saved.




Server Error in ‘/’ Application. Runtime Error on opening a SharePoint site

Got this error while opening a SharePoint site.

Changing custom error to Off it showed  the following error

Line 1:  <browsers>
Line 2:      <browser id="Safari2" parentID="Safari1Plus">
Line 3:          <controlAdapters>

Data at the root level is invalid. Line 1, position 1.

Deleted  the _vti_cnf folder of /App_Browsers/ of the site and everything was back to normal!!!

Difference between workflow created using SharePoint Designer and Visual Studio Designer for Windows Workflow Foundation.

SharePoint Designer

Visual Studio 2005 Designer for Windows Workflow Foundation.


Can write only sequential workflows.

Can write both sequential and state machine workflows.


Automatic deployment against the specific list or library against which workflow is being designed.

Can be deployed as a feature.

Logic is defined declaratively using Steps which comprises of Conditions and Actions

Logic could be defined through custom code written using C# or VB.NET.

Workflows could be associated to a specific list or library.

Workflow can be authored as Template which once deployed could be associated with any list or library.

Workflow modifications not possible.

Workflow modifications are possible using Modification forms built using ASP.NET or InfoPath form.

Workflow markup, rules all are stored as a document library on the site.

Workflows are compiled as an .NET assembly.

Can’t be debugged.

Debugging is possible using Visual Studio.

Using Workflow Object Model in SharePoint.

I was assigned a task to create a simple aspx page where the user could see all the all the different documents, workflows running against them, workflows task information as well as workflow history.

Here we can make use of SPWorkflow and SPWorkflowTask class.

The page would be displaying the information in the following manner

Workflow status for following document :-SampleDocument1
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Second Team Task
Third Team Task
Workflow History Description
The approval workflow has started waiting for and Second Team to respond
Task has been created and assigned to First and Second Team
First and Second team has completed their task

Workflow status for following document :- SampleDocument2
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Workflow History Description
The approval workflow has started waiting for First and Second Team to respond

The sample code for getting the above information

protected void Page_Load(object sender, EventArgs e)


//SPWorkflowManager myWFMgr = new SPWorkflowManager();

SPSite objSite = new SPSite(http://servername:port&#8221;);

SPWeb objWeb = objSite.OpenWeb();

SPList myList = objWeb.Lists[“ListName”];

// for each document within the Library

foreach (SPListItem myListItem in myList.Items)


Response.Write(“<b>Workflow status for following document :-</b>” + myListItem[“Title”].ToString());


// Get the workflows associated

foreach (SPWorkflow myWF in myListItem.Workflows)


// Get the name of the workflow

Response.Write(“Workflow name :- “+ myWF.ParentAssociation.Name);


Response.Write(“<b>Workflow Task Title </b>”);


// for each workflow running get the workflow tasks and history information

foreach (SPWorkflowTask myWFTask in myWF.Tasks)





// for each workflow running get the history information

Response.Write(“<b>Workflow History Description</b> “);


SPList myList1 = objWeb.Lists[“Workflow History”];

SPQuery query = new SPQuery();

query.Query = “<OrderBy><FieldRef Name=”ID”/></OrderBy>” +

“<Where><Eq><FieldRef Name=”WorkflowInstance”/>” +

“<Value Type=”Text”>{“ + myWF.InstanceId.ToString() + “}</Value>” +


SPListItemCollection historyListItems = myList1.GetItems(query);

foreach (SPListItem i in historyListItems)


Response.Write( i[“Description”].ToString());







That’s it …

Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property

I was writing a workflow using SharePoint designer wherein at certain steps tasks were getting created and assigned to different user. But the problem with that was that any user having appropriate rights on the tasks list was able to edit the task.

Below are the two methods using which we can have only the assigned to user having the rights on that task.

It can be done using Event Receiver or within the SharePoint workflow using special permissions property.

public override void ItemAdded(SPItemEventProperties properties)


                // Name of the List

            if (properties.ListTitle == “Tasks”)


                // Get the SPSite Object

                SPSite objSite = new SPSite(http://servername:portname&#8221;);       

                // Point to the top level web site within it

                SPWeb objWeb = objSite.OpenWeb();

                // get the task list item getting created

                SPListItem myListItem = properties.ListItem;       


                // get the id of the assigned to user

                // we want that only assigned to user should have full rights on that task

                string userAssignedTo=myListItem[“Assigned To”].ToString();

                int index = userAssignedTo.IndexOf(‘;’);

                int id = Int32.Parse(userAssignedTo.Substring(0, index));

                // get the SPUser from the id

                SPUser user = objWeb.SiteUsers.GetByID(id);                       


                // break the role inheritance


                // webroledefinitions – Full Right, Design, Contribute and Read

                SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;

                SPRoleAssignment roleassignment = new SPRoleAssignment(user);              

                roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);


                // give full control right to the assigned to user






Or within workflow as


    // handler for create task activity

        private void createTask1_MethodInvoking(object sender, EventArgs e)


            //Specify properties for the task

            createTask1.TaskProperties.AssignedTo = @”domainusername”;

            createTask1.TaskProperties.Title = @”Please complete the task”;

            createTask1.TaskProperties.Description = “This is sample SharePoint Task”;

            createTask1.TaskProperties.DueDate = DateTime.Now.AddDays(7);

            createTask1.TaskProperties.EmailBody = “This is the sample<b><i> email body </b></i>”;

            createTask1.TaskProperties.SendEmailNotification = true;


            // Define a HybridDictionary object

            HybridDictionary permsCollection = new HybridDictionary();

            // Give Administrator rights to the user to whom the task has been assigned

            permsCollection.Add(createTask1.TaskProperties.AssignedTo, SPRoleType.Administrator);

            // SpecialPermissions -the SpecialPermissions property  in your code will strip out all existing permissions inherited from

            // the parent list(Workflow Task List) and only adds permissions for each pair you added to the hashtable

            createTask1.SpecialPermissions = permsCollection;   





That’s it ….