Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Wednesday, November 30, 2011

Writing a Custom WebService for SharePoint

using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    //DataView dv;
    public Service()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public DataSet getTasks()
    {
        try
        {
            DataSet dv = new DataSet();
            DataTable dt = new DataTable();
            //dv = new DataView();
            SPSite obsite = new SPSite("http://vlnrm:100/Prasad");
            SPWeb currentWeb = obsite.AllWebs["Prasad"];
            dt.TableName = "Prasad";
            dt.Columns.Add("Title");
            dt.Columns.Add("Status");
            dt.Columns.Add("Priority");
            dt.Columns.Add("StartDate");
            SPList Tasks = currentWeb.Lists["Tasks"];
            string strTitle = string.Empty;
            if (Tasks != null)
            {
                SPListItemCollection listItems = currentWeb.Lists["Tasks"].Items;
                if (listItems.Count > 0)
                {
                    foreach (SPListItem item in listItems)
                    {
                        if (item["Title"] != null)
                        {
                            strTitle = item["Title"].ToString();
                        }
                        else
                        {
                            strTitle = "--";
                        }
                        DataRow drow = dt.NewRow();
                        drow["Title"] = strTitle;
                        drow["Status"] = item["Status"].ToString();
                        drow["Priority"] = item["Priority"].ToString();
                        drow["StartDate"] = item["StartDate"].ToString();
                        dt.Rows.Add(drow);

                    }
                    dv.Tables.Add(dt);
                }
            }
            return dv;
        }
        catch (Exception ex)
        {
            string str = ex.ToString();
            Console.WriteLine(str);
            DataSet dsErr = new DataSet(); ;
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("err");
            DataRow dr1;
            dr1 = dt1.NewRow();
            dr1["err"] = ex.ToString();
            dt1.Rows.Add(dr1);
            dsErr.Tables.Add(dt1);
            return dsErr;
        }
    }

}

No comments:

Post a Comment