Thursday, 29 November 2012


In certain situation we need to pass data from webpart to another webpart present in the same site. To implement the connection functionality between WebParts, we should create an Interface .This interface will be implemented by both the provider and consumer classes.
 
Then we will use the ConnectionProvider and ConnectionConsumer attribute to make the connection between the two webpart.

EXAMPLE:
1.  Interface :  
 
    
public interface Test
    {
        string Name { get; set; }
    }
 
 
2. The provider class :
 
    public class Provider : WebPart, Test
    {
        TextBox tb = new TextBox();
        Button btn = new Button();
        string _Name = "";
 
        public string Name
        {
            get{return _Name;}
            set{_Name = value;}
        }      
       
        protected override void CreateChildControls()
        {
            btn.Text = "Show";
            btn.Click += new EventHandler(btn_Click);
            tb.Text = "Prem";
            Name = tb.Text;
            this.Controls.Add(tb);
            this.Controls.Add(btn);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();
            RenderChildren(writer);
 
        }
        void btn_Click(object sender, EventArgs e)
        {
            Name = tb.Text;
        }
     
        [ConnectionProvider("Test Provider")]
        public Test ProvideCommunication()
        {
            return this as Test;
        }  
      }
3.The consumer class:
 
     public class Consumer : WebPart
    {
        private Test providerWebPart;
 
        [ConnectionConsumer("Test Consumer")]
        public void ReceiveName(Test  provider)
        {
            providerWebPart = provider;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (providerWebPart != null)
                writer.Write("Value provided is : " + providerWebPart.Name);
            base.Render(writer);
        }
    }

When the button is clicked the value for  the Name is set. Then the current instance of the interface is pass to consumer webpart. The ReceiveName Method will  act as a receiver of the Test interface.

Author: Sreekanth Reddy M



In SharePoint, View is a virtual representation of a set of data from a specific list. We can programmatically create and update views for a list.
Provided below is an example in C#.Net which explains the approach of creating and updating views. The code is divided in to 2 parts i.e. Part I - describes how to create a view by assigning specific fields from a list, and Part II - describes how to update an existing view by adding few more fields to it.
using (SPSite oSPsite = new SPSite("http://website_url/"))
{
    oSPsite.AllowUnsafeUpdates = true;
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
        oSPWeb.AllowUnsafeUpdates = true;
        /* get the list instance by list name */
        SPList list = oSPWeb.Lists["List_Name"];
        /* ======== Part I (create a view) ======== */
        // add the field names to the string collection
        StringCollection strViewFields = new StringCollection();
        strViewFields.Add("FullName");
        strViewFields.Add("Address");
        strViewFields.Add("City");
        strViewFields.Add("State");
        // create a standard view with the set of fields defined in the collection
        list.Views.Add("View_Name", strViewFields, String.Empty,
            100, truefalseSPViewCollection.SPViewType.Html, false);
        /* ==== End Part I ==== */
        /* ==== Part II (add fields to an existing view) ==== */
        // get the view instance by view display name
        SPView view = list.Views["Existing_View_Name"];
        // add fields to the view
        view.ViewFields.Add("Zip");
        view.ViewFields.Add("Country");
        // update view for new fields
        view.Update();
        /* ==== End Part II ==== */
        /* update the list */
        list.Update();
        oSPWeb.AllowUnsafeUpdates = false;
    }
    oSPsite.AllowUnsafeUpdates = false;
}

Author: Sreekanth Reddy M



Using Microsoft Sharepoint APIs we can easily Add, Update and Delete list items programmatically. Provided below is a code snippet in C# .Net demonstrating all the three operations..
using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
            oSPWeb.AllowUnsafeUpdates = true;
 
            // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];
                   
            //Add a new item in the List
            SPListItem itemToAdd = list.Items.Add();
            itemToAdd["Title"] = "Test Title";
            itemToAdd["Description"] = "Test Description";
            itemToAdd.Update();
 
            // Get the Item ID
            listItemId = itemToAdd.ID;
 
            // Update the List item by ID
            SPListItem itemToUpdate = list.GetItemById(listItemId);
            itemToUpdate["Description"] = "Changed Description";
            itemToUpdate.Update();
 
            // Delete List item
            SPListItem itemToDelete = list.GetItemById(listItemId);
            itemToDelete.Delete();
 
            oSPWeb.AllowUnsafeUpdates = false;
       }
}

Thanks,
Sreekanth Reddy M