SharePoint 2010 Connected Web Parts

In this article we can explore Connected Web Parts which is an advanced SharePoint Web Part feature. The connected web parts denote communication between 2 web parts.

CntWbShare1.jpg

The connected web part resolves the following requirements:
  • Master Web Part and Slave web part data exchange
  • Data Selection and Data Display web part communication
Summary of the application is:
  • There are 2 web parts
  • Web Part 1 has a drop down list for selecting Car
  • Web Part 2 displays the car picture based on selection
Steps Involved

Following are the steps involved in creating connected web parts:
  1. Create Project
  2. Create Communication Interface
  3. Create Web Part 1 (Provider) implementing Interface
  4. Create Web Part 2 (Consumer)
  5. Build and Deploy
Create Project

We can experiment with a Car showcase application where there are 2 web parts.
  • The first web part allows selecting a Car from the drop down list.
  • The second web part displays the image of the car.
to begin, create a new Web Part project in Visual Studio.

CntWbShare2.jpg

Remove the default VisualWebPart1 web part. We will be adding our own web parts.

Create Interface

For communication between the 2 web parts we need to create an interface. Please create the following interface, which returns the selected Car name:
public interface ICar{
    string Car { get; }
}


Create Provider Web Part implementing Interface

Now you can create a new web part named CarSelectionWebPart. Expand the webpart item and in the User Control (CarSelectionWebPartUserControl.ascx) class file:
  1. Add a drop down list control
  2. Place the following code
public partial class CarSelectionWebPartUserControl : UserControl{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DropDownList1.Items.Clear();
            DropDownList1.Items.Add("Camry");
            DropDownList1.Items.Add("Figo");
            DropDownList1.Items.Add("Diablo");
        }
    }
    // Property to expose selected car outside    public string Car
    {
        get;
        set;
    }
    // Set the Car property    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem != null)
            Car = DropDownList1.SelectedItem.Text;
    }
}


In the CarSelectionWebPart.cs (web part class file) add the following code:
 
// Property to return the selected Car
public string Car
{
    get
    {
        return (_control as CarSelectionWebPartUserControl).Car;
    }
}

// Special Purpose Attribute to denote ICar interface is provided
[ConnectionProvider("Car")]
public ICar GetCar()
{
    return this;
}
The ConnectionProvider attribute and the interface of the method denotes that this web part provides data for ICar interface. The Connection Provider also says that the name is Car (in string).

Create Consumer Web Part

Now we can create the consumer web part which will get the selected Car to display the car picture. Add a new web part and name it CarDisplayWebPart. Place an image control on the user control of the web part. In the class view of the user control add the following method:
 
// Set the image to url
public void SetImage(string car)
{
    if (car == "Camry")
        Image1.ImageUrl = "/_layouts/images/ConnectedWebParts/Camry.png";

    else if (car == "Figo")
        Image1.ImageUrl = "/_layouts/images/ConnectedWebParts/Figo.png";
           
    else if (car == "Diablo")
        Image1.ImageUrl = "/_layouts/images/ConnectedWebParts/Diablo.png";
}

Now add Images Mapped Folder and place 3 image files (Camry.png, Figo.png, Diablo.png) in the folder as shown below:

CntWbShare3.jpg

Build and Deploy

Now build the project and use the Deploy command. Open the SharePoint site in edit mode and add the above 2 web parts into it. You are now ready with the following 2 web parts.

CntWbShare4.jpg

Set Connection

Use the context menu of CarDisplayWebPart to set the connection to provider web part.

CntWbShare5.jpg

Testing the Web Parts

Now stop editing the web page and refresh the site. Select a car from the first web part and the car picture is displayed on the second web part.

CntWbShare6.jpg

This concludes our article on Connected Web Parts creation.

Debugging the Web Parts

You can debug the application, by executing the project in Debug mode. Any break points set will be executed.

CntWbShare7.jpg

References
http://msdn.microsoft.com/en-us/library/ms469765.aspx 
http://rtmgroupq8.com/en/add-image-to-sharepoint-visual-web-part 

Summary

In this article we have explored the Connected Web Parts feature of SharePoint Web Parts. In real-world scenarios there could be multiple web parts communicating with each other through connected web parts. SharePoint also supports AJAX enabled web parts which provide a better communication between web parts. The attachment contains the source code of the web parts we discussed.
 

How to set default/custom master Page in sharepoint programatically using VS Studio

Recently i received number of request to post on “How to set custom/default master page programatically using Visual Studio 2010″. So this post is for those people who desperately waiting to have something into their plate  :)
Goal: You can set a custom/default master page using SharePoint Designer 2010 as well. But you need to work on Designer for that. So to remove this dependency, one solution is to create a solution project which allows you to set custom/default master page programatically.
I am explaining all steps which deploys the custom/default master page and set as default as well. The steps are as below:
1. Create module and add master page
2. Set added master page as default master page
1. Create module and add master page:
  • Open Visual Studio 2010 –> Add New Project –> Select “Empty SharePoint Project” –> Select “Deploy as Farm Solution” –> Click on “Finish”
  • Right Click on Solution and select Add new item –> Select “Module” and name it what you want. (In my case i used CustomMasterPage)
  • Delete “Sample.txt” file or rename it with “MyCustomMasterPage.master”
  • Open “Element.xml” file of “CustomMasterPage” module.
  • Element.xml file looks like below after doing some changes :
  • Please make sure you have entered below values and deploy your solution
      List=116Url=_catalogs/masterpage“    and    Url=MyCustomMasterPage.masterType=GhostableInLibrary
2. Set added master page as default master page
  • From solution project select “Features –> Feature1 –> Feature1.feature” –> Right click “Add event receiver”
  • Add below mentioned code and deploy solution.
You are done with the mentioned goal.
But there is still one issue. When you need some changes into custom master page and want to re-deploy it, Your updated master page would not get deployed. So to resolve this issue, we need to write de-activating event which reset V4.master as default master page. Add below mentioned code in FeatureDeactivating event.
After adding this code Deploy your solution and this will reset V4.master page as default.

SharePoint Handling page request in IIS

The SharePoint 2010 Architecture
Unlike Asp.net, a SharePoint website is distributed among 3 logical parts, namely:-
1) IIS - Where website are create for each web application.
2) 14 Hive - Store configuration files, templates and other components for supporting SharePoint 2010.
3) Content Database - Back end server for each web application.

Lets take an example of the web application("http://home:3000/") we create in the previous post to verify these logical sections.

In central admin you can see the web application named "SharePoint : 3000"

Now lets verify this web application on each of the logical sections.
1) The IIS (inetmgr.exe)
Just like a normal asp.net website, virtual directories are created for each web application in sharePoint. This directories consists of the .aspx pages, web.config, short-cuts to 14 hive folders like _layouts, _vti_bin etc. In the following figure you can see a virtual site named "SharePoint : 3000" has been created.

2) The 14 Hive ("C:\Program Files\Common files\Microsoft Shared\Web Server Extensions\14 ")
All the configuration files like web.config, feature.xml, etc reside in 14 Hive. It also consist of all the in-built SharePoint master-pages, styles, site definitions, etc. Any custom developed component like webparts, user controls, application pages etc is deployed in 14 hive.
The files in hive are accessed by IIS through shortcuts configured by sharePoint inside the virtual directories.
None of the web application related folders are created in 14 hive.
3) The Content Database
This is back-end database base where all the SharePoint data is stored. List Items, Document library contents, site pages etc are stored in the content database. In the following figure you can see a database named "WSS_Content_3000" exclusively created for "SharePoint : 3000" web application.

How does IIS handle a SharePoint request
1) A request for SharePoint page is sent through browser arrives at HTTP.sys in IIS. The HTTP.sys determines if the request is valid. If the request is not valid, it sends a code for an invalid request back to the client.
2) An IIS website monitors for incoming requests through a particular port, and it checks for a particular host header or IP address, or both. Every SharePoint Foundation web application is hosted in an IIS website that has the same name as the web application.
3) The request is passed through a pipeline of units including authenticating the user, verifying the user’s authorization, building the response, sending the response, and finally, logging the request.

4) The response to any request is produced by an HTTP handler object. Requests are assigned to one or another HTTP handler object depending on the resource requested and the HTTP verb in the request.

5) The request pipeline also contains HTTP modules. Modules are assemblies that typically contain one or more event handlers or define new events that other modules can handle. An HTTP module can register for one or more events in the life cycle of the request.

6) The processing of a request by HTTP modules and an HTTP handler is governed by an HttpApplication object or an object derived from that class. SharePoint installs a global.asax file in the root of each web application (IIS website) that identifies SPHttpApplication as the class from which an HTTP application object is created.

7) SharePoint also modifies the pipeline with a custom HTTP module (SPRequestModule), and with changes to the default applicationhost.config file.

8) SPRequest module eventually request the location of the SharePoint configuration database from the registry. Connection string for the SharePoint configuration database is set using DSN key at  HKLM\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\Secure\ConfigDB.
So, this is the registry value, you need to change, if you ever need to move configuration database.

9) When an HTTP request is received by a front-end web server, a special driver, HTTP.SYS, detects the request and routes it to the application pool that is handling requests for the targeted IIS website, and thus, the targeted SharePoint web application. Every application pool has an IIS worker process (w3wp.exe) in which the request pipeline for each request is executed.

10) After processing the request, the worker process sends the response back to HTTP.sys which in turn sends the response back to the client and logs the request, if configured to do so.

References:
1) http://msdn.microsoft.com/en-us/library/gg552610(v=office.14).aspx
2)http://sptechbytes.blogspot.in/2013/01/sharepoint-2010-handling-page-request.html

site workflow vs list workflow vs reuasble workflow

List WorkFlow Vs Reusable WokFlow Vs Site WorkFlow


Today we will discuss about the differences between the List Workflow Vs reusable Workflow vs Site Workflow.

List Workflow:Associated with one particular List

in the below screen you can this associated with List called:DevendraList
observe the Ribbon few options are disabled we will discuss in Reusable workflow.

1.can't publish it globally.
2.can't save it as template .
2.can't associate to any content type.



check the start options available for List workflow from SharePoint designer.



Reusable Workflow:Associate to Content Type.so it is reusable.you can attach it to any list or library.

we can publish the Reusable workflow Globally .
Save it is as Template ,it will create the WSP file.
we can associate to Content type.



check the start options available for Reusable workflow from SharePoint designer.
you can disable the workflow option.



Site Work Flow::you can run anywhere in the site .



check the start options available for Site workflow from SharePoint designer.
Site workflow can be always started manually.