Thursday, January 6, 2011

Creating a connectable webparts

This is very nice post  for creating connectable web Parts.
Have a look at link below:
Creating a connectable webparts

Creating Simple WebPart using Visual Studio 2008 extensions for Windows SharePoint Services 3.0

Environment:
Moss2007, Visual Studio2008 and
Visual Studio 2008 extensions for Windows SharePoint Services 3.0 (Download here)

Steps:
1.Create a new project and click  Sharepoint and then select Web Part as  shown in Fig 1.

                                                                     Fig 1
2.Solution Explorer after previous step should look like as in Fig 2.
                                                                     Fig 2
Open the cs file and uncomment the label options under CreateChildControls as shown above.

3. Goto Project under Menubar and select webpart properties at the bottom.
4.Under Start With URL option under Debug option Type the address of your site as shown in Fig 3
Fig 3

5. Press F5, to Run the project.
6.Open the Sharepoint site and select site Settings under Site Actions on top right corner of sharepoint home 
   page.It will display page as shown in Fig 4.

Fig 4
   You can see your added web part in webpart gallery after clicking Web Parts option as shown in Fig4.

7.Goto home page and click Edit Page option under Site Actions. After this home page ill look as in Fig 5.
                                                             Fig 5
Click  Add Web Part based on location where you want to add it and you are DONE.





Wednesday, January 5, 2011

Sharepoint 2007 - Get data using web services

Sharepoint 2007/ MOSS 2007  - Getting/ Reverting data stored in list using web services

This program demonstrates about how to retrieve data from lists at sharepoint server into your website using lists web service .This Demonstration consists of two parts .In first part we create a list in sharepoint site and add items in that list. In second part we create a simple web site and this web site retrieve list data from share point server using Lists web service.

PART I

Steps
1 . At sharepoint site create a list named product with folloing fields

                                                                               Figure 1
Steps for creating sharepoint list are: 
   1.1 Click on Site Actions on top right corner of Figure 1.
   1.2 Click on Create option.
   1.3 Select custom List under Custom Lists option.
   1.4 For  adding your own columns .Click on Settings option in Figure1 and click on Create Column option   in Column.

2. Adding items in list
        Click on New option in Figure1 and select Additems.



PART II

Steps

1.Create a website in visual studio with Default.aspx design as shown: This  web site will be accessing the product list data in following manner.

                                                                      Figure 2
 This  web site will be accessing the product list data in following manner.  Upon selecting the category, subcategory will automatically be updated and the corresponding product details will be displayed in products table.Ctegory ,Subcategory and products detail table data is stored in list created in sharepoint webserver.
2.Right click in solution bar and select Add Web Reference option as shown:

                            Figure 3
This is the way to access lists web service at sharepoint server.In URL textbox type
i.e http://sereverName/sites/siteName/_vti_bin/lists.asmx?WSDL as shown in Figure 4

                                                                    Figure 4

After successfull adding reference Solution Explorer will look as shown in Figure 5.
                                                                         Figure 5

2. Code for the Program
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Net;
using System.Web.UI.HtmlControls;
using System.Xml;
using win_kqs5crbcani;      // NOTE :Web Service name

public partial class _Default : System.Web.UI.Page
{
    private const string url = "http://win-kqs5crbcani/sites/Eknoor";
    private         Lists proxy;
    protected void Page_Load(object sender, EventArgs e)
    {
        proxy = new Lists();
        proxy.Url = url + "/_vti_bin/lists.asmx";
        proxy.Credentials = CredentialCache.DefaultCredentials;

        if (!Page.IsPostBack)
          PopulateCategories();  
    }

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    { }


protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
    {
        PopulateSubCategories();
    }

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        PopulateProducts();
    }

    private void PopulateCategories()
    {
       
XmlDocument doc = new XmlDocument();
doc.LoadXml("<ViewFields><FieldRef Name='Category' /><FieldRef Name='Title' /></ViewFields>");

    XmlNode results =proxy.GetListItems("product",null,null,doc.DocumentElement,null,null,null);

XmlNodeReader nr = new XmlNodeReader(results);
DataSet ds = new DataSet("resultdataSet");
        ds.ReadXml(nr);
        nr.Close();

        DataView dv = new DataView(ds.Tables[1]);
        DataTable dt = dv.ToTable(true,"ows_category");
       
        DropDownList1.DataTextField = "ows_category";
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();

        PopulateSubCategories();
   
    }

    private void PopulateSubCategories()
    {
        string category = DropDownList1.SelectedItem.Text;
        XmlDocument fieldsDoc = new XmlDocument();
        fieldsDoc.LoadXml("<ViewFields><FieldRef Name='SubCategory' /><FieldRef Name='Title' /></ViewFields>");
        string query = "<Query><Where><Eq><FieldRef Name = 'Category'/><Value Type = 'Text'>{0}</Value></Eq></Where></Query>";
        query = string.Format(query, category);
        XmlDocument queryDoc = new XmlDocument();
        queryDoc.LoadXml(query);

        XmlNode result = proxy.GetListItems("product", null,queryDoc.DocumentElement,fieldsDoc.DocumentElement,null, null, null);
        XmlNodeReader nr = new XmlNodeReader(result);
        DataSet ds = new DataSet("result");
        ds.ReadXml(nr);
        nr.Close();

        DataView dv = new DataView(ds.Tables[1]);
        DataTable dt = dv.ToTable(true, "ows_subcategory");

        DropDownList2.DataTextField = "ows_subcategory";
        DropDownList2.DataSource = dt;
        DropDownList2.DataBind();
      
        PopulateProducts();
    }


    private void PopulateProducts()
    {
        string category = DropDownList1.SelectedItem.Text;
        string subcategory = DropDownList2.SelectedItem.Text;
        XmlDocument fieldsDoc = new XmlDocument();
        fieldsDoc.LoadXml("<ViewFields><FieldRef Name='ProductName' /><FieldRef Name='ProductNumber' /></ViewFields>");
        string query = "<Query><Where><And><Eq><FieldRef Name='Category'/><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='SubCategory'/><Value Type='Text'>{1}</Value></Eq></And></Where></Query>";
       
        query = string.Format(query,category, subcategory);
        XmlDocument queryDoc = new XmlDocument();
        queryDoc.LoadXml(query);

        XmlNode result = proxy.GetListItems("product", null, queryDoc.DocumentElement, fieldsDoc.DocumentElement, null, null, null);
        XmlNodeReader nr = new XmlNodeReader(result);
        DataSet ds = new DataSet("resultdataSet");
        ds.ReadXml(nr);
        nr.Close();

       DataView dv = new DataView(ds.Tables[1]);
        DataTable dt = dv.ToTable(true, new string[] { "ows_ProductName"});
        Session["TblData"] = ds.Tables[1];
        GridView1.DataSource = ds.Tables[1].DefaultView;
        GridView1.DataBind();
   
    }
   }


POINTS TO NOTE

1.Data is retrieved by using CAML query.
2.Web Service name extension is asmx. (Common mistakes done by beginners)
3. asmx files are available from http://siteUrl/ _vti_bin ,_vti_bin is a virtual directory pointing to  ..\12\ISAPI

-Happy Coding
Parminder Josan