Monday, August 15, 2011

SHARE POINT PROGRAMS WITH NEAT STEP BY STEP CODE


WSS Object Model

1. SPSite Class
Represents a collection of sites in a Web application, including a top-level Web site and all its subsites
2. SPWeb Class
Represents a Windows SharePoint Services Web site.
3. SPWebCollection Class
Represents a collection of SPWeb objects.
4. SPList Class
Represents a list on a SharePoint Web site.
5. SPListCollection Class
Represents a collection of SPList objects.
6. SPListItem Class
Represents an item or row in a list.
7. SPListItemCollection Class
Represents a collection of SPListItem objects.
8. SPListTemplateType Enumeration
Specifies the type of a list definition or a list template.
9. SPView Class
Represents a view of the data that is contained in a list on a SharePoint site.
10. SPViewCollection Class
Represents a collection of SPView objects.
11. SPQuery Class
Represents a query in a list view
13. SPField Class
Represents a field in a list on a Windows SharePoint Services Web site.
14. SPFieldCollection Class
Represents a collection of SPField objects.
15. SPUser Class
Represents a user in Microsoft Windows SharePoint Services.
17. SPUserCollection Class
Represents a collection of SPUser objects.
18. SPGroup Class
Represents a group on a Windows SharePoint Services Web site
19. SPGroupCollection Class
Represents a collection of SPGroup objects.
20. SPFolder Class
Represents a folder on a SharePoint Web site.
21. SPFolderCollection Class
Represents a collection of SPFolder objects.
22. SPFile Class
Represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.
23. SPFileCollection
Represents a collection of SPFile objects.
24. SPForm Class
Represents a form creating, displaying, or editing a list item in a list.
25. SPFormCollection Class
Represents a collection of SPForm objects



// 1. Program to Get Properties of SharePoint Site
using Microsoft.SharePoint;
public partial class DisplaySiteProp : System.Web.UI.Page
{
    SPSite spsite = new SPSite("http://system:9000/");
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            spweb = spsite.OpenWeb();
            //Display Current User
            string sCurrentUser = spweb.CurrentUser.ToString();
            Response.Write("The Current User is:" + sCurrentUser + "<br/>");
            //Display title
            string sTitle = spweb.Title;
            Response.Write("Title is:" + sTitle + "<br/>");
            //Display all sites in site collection
            SPWebCollection spwebcoll = spsite.AllWebs;
            Response.Write("The sites in site collection are" + "<br/>");
            for (int icount = 0; icount < spwebcoll.Count; icount++)
            {
                Response.Write(spwebcoll[icount].Title + "<br/>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {           
            spsite.Close();
            spweb.Close();
            spsite.Dispose();
            spweb.Dispose();
        }
    }
}

//2. Program to create Sub Site Creation
using Microsoft.SharePoint;
public partial class CreateSite : System.Web.UI.Page
{
    SPSite spsite = new SPSite("http://system:9000/");
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spweb = spsite.OpenWeb();
    }
    string sTitle, sName, sDesc, sTemplate;
    protected void btnCreateSite_Click(object sender, EventArgs e)
    {
        try                                
        {
            sTitle = txtTitle.Text;
            sName = txtName.Text;
            sDesc = txtDesc.Text;
            sTemplate = spweb.WebTemplate;
            SPWebCollection spwebcoll = spsite.AllWebs;
            spwebcoll.Add(sName, sTitle, sDesc, 1033, spweb.WebTemplate,   
                          true, false);
            Response.Write("<font size=8>Site Created Successfully</font>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            spsite.Close();
            spweb.Close();
            spsite.Dispose();
            spsite.Dispose();
        }
    }
}

// 3. Program  to display List available in site
using Microsoft.SharePoint;
public partial class DisplayList : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            spsite = new SPSite("http://system:9000/");
            spweb = spsite.OpenWeb();
            SPListCollection lstcoll = spweb.Lists;
            Response.Write("Lists in sites");
            foreach (SPList lst in lstcoll)
            {
                Response.Write(lst.Title + "<br/>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {  
            spsite.Close();
            spweb.Close();
            spsite.Dispose();
            spweb.Dispose();

        }    }  }

// 4. Program to List Creation
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://system:9000/");
        spweb = spsite.OpenWeb();
    }
    protected void create_Click(object sender, EventArgs e)
    {
        string sName = txtName.Text;
        string sDesc = txtDesc.Text;
        string sType = ddlType.SelectedItem.ToString();
        try
        {
            SPListCollection lstColl = spweb.Lists;
            SPListTemplateType tempType = new SPListTemplateType();
            switch (sType)
            {
                case "Announcements": tempType =
                         SPListTemplateType.Announcements;
                    break;
                case "Tasks": tempType = SPListTemplateType.Tasks;
                    break;
                case "GenericList": tempType =                                           
                                  SPListTemplateType.GenericList;
                    break;
            }
            spweb.AllowUnsafeUpdates = true;
            lstColl.Add(sName, sDesc, tempType);
            Response.Write("list of type" + sType + "is created");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {           
            spsite.Close();
            spweb.Close();
            spsite.Dispose();
            spweb.Dispose();
        }
    }  
}


// 5. Program  to Edit List Items
using Microsoft.SharePoint;
public partial class EditItems : System.Web.UI.Page
{
    SPSite spsite = new SPSite("http://system:9000/");
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            spweb = spsite.OpenWeb();
            SPList splist = spweb.Lists["Employee"];
            SPListItem itmToEdit = splist.GetItemById(1);
            itmToEdit["Title"] = "101-edit";
            itmToEdit["EName"] = "abc-edit";
            spweb.AllowUnsafeUpdates = true;
            itmToEdit.Update();
            splist.Update();
            spweb.Update();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {  
            spsite.Close();
            spweb.Close();  
            spsite.Dispose();
            spweb. Dispose ();        
        }
    }
}
// 6. Program to Display List Items
using Microsoft.SharePoint;
public partial class DisplayListItems : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            spsite = new SPSite("http://system:9000/");
            spweb = spsite.OpenWeb();
            SPList lst=spweb.Lists["Employee"];
            SPListItemCollection itemcoll = lst.Items;
            Response.Write("List of Employees are:" + "<br/>");
            foreach (SPListItem item in itemcoll)
            {
                Response.Write(item["Title"] + "   "+item["EName"]+"<br/>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            spsite.Close();           
            spweb.Close();
            spweb.Dispose();
            spsite.Dispose();                     
        }
    }}

// 7.Program to Delete Sites in a Site Collection
using Microsoft.SharePoint;
public partial class DeleteSite : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    SPWebCollection spwebcoll;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://technofin:9000/");
        spweb = spsite.OpenWeb();
        spwebcoll = spsite.AllWebs;
        foreach (SPWeb web in spwebcoll)
        {
            ListItem li = new ListItem();
            li.Text = web.Name;
            li.Value = web.Name;
            ddlSitestoDelete.Items.Add(li);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        spwebcoll.Delete(ddlSitestoDelete.SelectedValue);
        lblMsg.Text = "The site is deleted";
    }
}
// 8. Program to add Item to list
using Microsoft.SharePoint;
public partial class Lists_AddItems : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://system:9000");
        spweb = spsite.OpenWeb();
        spweb.AllowUnsafeUpdates = true;
        SPList splist= spweb.Lists["Employee"];
        SPListItemCollection lstcoll = myList.Items;
        SPListItem newItem = lstcoll.Add();
        newItem["Title"] = "105";
        newItem["EName"] = "techno";
        newItem.Update();
        splist.Update();
        spweb.Update();
    }
}

// 9. Program  to Create view for list
using System.Collections.Specialized;
using Microsoft.SharePoint;
public partial class AddViews : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://system:9000");
        spweb = spsite.OpenWeb();
        SPList splist = spweb.Lists["Employee"];
        SPViewCollection viewColl = splist.Views;
        StringCollection fldsinView = new StringCollection();
        fldsinView.Add("EmpID");
        fldsinView.Add("EName");
        fldsinView.Add("Empsal");
        spweb.AllowUnsafeUpdates = true;
        viewColl.Add("Prg_View", fldsinView, "", 100, false, true);
        splist.Update();
        Response.Write("New View Added");
    }
}

// 10. Program to Create Document Library
using Microsoft.SharePoint;
public partial class Libraies_Create_Library : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://system:9000");
        spweb = spsite.OpenWeb();
        SPListCollection lstColl = spweb.Lists;
        spweb.AllowUnsafeUpdates = true;
        lstColl.Add("Prg_DocLib", "Sample Library From Program",  
        SPListTemplateType.DocumentLibrary);
        spweb.Update();
        Response.Write("New Library Added");
    }
}



// 11. Program to See Documents properties in Document Library
using Microsoft.SharePoint;
public partial class Libraies_ViewProperties : System.Web.UI.Page
{
    SPSite spsite;
    SPWeb myWeb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spsite = new SPSite("http://system:9000");
        myWeb = spsite.OpenWeb();
        SPFolder spDocLib = myWeb.GetFolder("Shared Documents");
        SPFileCollection spFiles= spDocLib.Files;
        foreach (SPFile spFile in spFiles)
        {
            Response.Write("<b>" + "Name of the File" +spFile.Name+ "</b><br/>");
            Response.Write("Author" + spFile.Author + "<br/>");
            Response.Write("Size" + spFile.Length + "<br/>");
        }
     }
}

Collaboration Apllication Markup Language (CAML) examples

// 12. Program using CAML Query

using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
    SPSite spsite = new SPSite("http://system:9000");
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spweb = spsite.OpenWeb();
        SPList splst = spweb.Lists["employeeInfo"];

        SPQuery spquery = new SPQuery();
       
        spquery.Query = string.Format("<OrderBy><FieldRef   
        Name='Employee_x0020_Name'/></OrderBy>");

        SPListItemCollection itemcoll = splst.GetItems(spquery);

        Response.Write("<h1>" + "List Items" + "</h1></br>");

        foreach (SPListItem item in itemcoll)
        {
            Response.Write(item["Title"]+"      "+item["Employee Name"] +   
             "<br/>");
        }
    }
}




// 13. program to display List in a Gridview

using Microsoft.SharePoint;
public partial class Default2 : System.Web.UI.Page
{
    SPSite spsite = new SPSite("http://system:9000");
    SPWeb spweb;
    protected void Page_Load(object sender, EventArgs e)
    {
        spweb = spsite.OpenWeb();
        SPList splst = spweb.Lists["employeeInfo"];

        SPQuery spquery = new SPQuery();
        spquery.Query = string.Format("<Where><Gt><FieldRef
         Name='Salary'/><Value Type='Currency'>15000</Value></Gt></Where>");

        SPListItemCollection itemcoll = splst.GetItems(spquery);

        Response.Write("<h1>" + "List Items" + "</h1></br>");

        DataTable dtItems = itemcoll.GetDataTable();

        gvItems.DataSource = dtItems;
        gvItems.DataBind();
    }

WebService Example

// 14. Program to display List Names
public partial class Lists : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ListsWebService.Lists myLstWebServ = new ListsWebService.Lists();       
       
        myLstWebServ.Credentials =  
        System.Net.CredentialCache.DefaultCredentials;      
       
        myLstWebServ.Url="http://system:2525/_vti_bin/Lists.asmx";
       
        System.Xml.XmlNode mynode=myLstWebServ.GetListCollection(); 
    
        Response.Write("<h2>"+"The Lists in the list coll are"+"</h2><br/>");
       
        foreach (System.Xml.XmlNode node in mynode)
        {
            Response.Write(node.Attributes["Title"].Value+"<br/>");
        }
    }
}


// 15.Program to add User to Group

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        usergrpwebservice.UserGroup webserv = new 
                                             usergrpwebservice.UserGroup();
        webserv.Credentials = System.Net.CredentialCache.DefaultCredentials;
        webserv.Url = "http://system:2525/_vti_bin/UserGroup.asmx";  
        webserv.AddUserToGroup("Viewers","manju","system\\manju",
                                        "manju@gmail.com","Hi..am Manjunath");                
        Response.Write("Users Added");
    }
}



No comments:

Post a Comment