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=http://schemas.microsoft.com/sharepoint/

SolutionId=96A76C66-7258-4721-BEB4-90C06E656DB6>

                <Assemblies>

                                <Assembly DeploymentTarget=WebApplication

                                Location=DeploymentWebPart.dll>

                                                <SafeControls>

                                                                <SafeControl

                                                                Assembly=DeploymentWebPart, Version=1.0.0.0, Culture=neutral,

PublicKeyToken=b0d80600de0f4a2b

                                                                Namespace=DeploymentWebPart TypeName=*/>

                                                </SafeControls>

                                </Assembly>

                </Assemblies>

</Solution>

 

 

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.

[Guid(“56978c39-1958-4128-a979-b9feaf2feb46”)]

    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.

[Guid(“3575c6de-e21a-4e5a-b7f0-fe1aa4844402”)]

    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)

        {

            try

            {  

                // priting the value provided by provider web part

                writer.Write(myProviderInterface.ProviderStringInfo);

            }

            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.

OnLoad

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

drawn.

RenderContents – Html Output is generated.

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

Dispose

UnLoad.

Bye…

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());

Response.Write(“</br>”);

// 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(“</br>”);

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

Response.Write(“</br>”);

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

foreach (SPWorkflowTask myWFTask in myWF.Tasks)

{

Response.Write(myWFTask[“Title”].ToString());

Response.Write(“</br>”);

}

// for each workflow running get the history information

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

Response.Write(“</br>”);

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>” +

“</Eq></Where>”;

SPListItemCollection historyListItems = myList1.GetItems(query);

foreach (SPListItem i in historyListItems)

{

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

Response.Write(“</br>”);

}

}

Response.Write(“</br>”);

}

}

That’s it …

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓