Pages

Wednesday 14 August 2013

Full URL of your SharePoint 2013 site in JavaScript


If you want to use the Full URL of your SharePoint site in JavaScript, you can simply use the below window property. "window.location.protocol + '//' + window.location.host;"
For site collection and webs Server Relative URL use "_spPageContextInfo.siteServerRelativeUrl" and "_spPageContextInfo.webServerRelativeUrl"


Read more: Learning SharePoint

Sunday 26 August 2012

Binding Data from SharePoint List to DataTable/ Grid View

Please Check the below complete code to bind SharePoint list items to a DataTable/ Gridview. In this post, we are using the list named "Employee" with 5 column: Title, Birthday, Male, Position, Salary. At first, we need to create an empty DataTable with its column
       

 protected DataTable dataTableInitiate()
      {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(string));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("Birthday", typeof(string));
            dt.Columns.Add("Male", typeof(string));
            dt.Columns.Add("Position", typeof(string));
            dt.Columns.Add("Salary", typeof(string));                     
            return dt;
      } 


For binding the item from the SharePoint list to the DataTable that we have just created:
       

protected DataTable bindToDataTable(SPListItemCollection itemCol)
      {
            DataTable dt = dataTableInitiate();
            if (itemCol.Count > 0)
            {
                foreach (SPListItem item in itemCol)
                {
                    DataRow dr = dt.NewRow();
                    dr["ID"] = int.Parse(item["ID"].ToString());
                    dr["Title"] = item["Title"] != null ? item["Title"].ToString() : string.Empty;
                    dr["Birthday"] = item["Birthday"] != null ? item["Birthday"].ToString() : string.Empty;
                    dr["Male"] = item["Male"].ToString() == "True" ? "Yes" : "No";
                    dr["Position"] = item["Position"] != null ? item["Position"].ToString() : string.Empty;
                    dr["Salary"] = item["Salary"] != null ? item["Salary"].ToString() : string.Empty;                    
                    dt.Rows.Add(dr);
                }
            }
            return dt;
       } 


We've completed copying data from SharePoint list items to DataTable. We'd like to bind data from this DataTable to the GridView control.
       

protected void bindToGrid()
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Employee"];
            SPListItemCollection items = list.Items;
            DataTable dt = new DataTable();
            dt = bindToDataTable(items);
            grid.DataSource = dt;
            grid.DataBind();
        }


Hope this helps!

Retrieve SharePoint List Data and bind this to a dropdownlist

To bind the drop down list data with a field from the SharePoint list you can use the below code methods:-
Method 1
       
if (!Page.IsPostBack)
        {
            using (SPSite site = new ("http://yoursharepointsite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["NameOfYourList"];
                    dropSite.DataSource = list.Items;
                    dropSite.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                    dropSite.DataTextField = "Title"; // List field holding name to be displayed on page 
                    dropSite.DataBind();
                }
            }
        }



Method 2:- Using SPDataSource


Popular Posts