Search This Blog

Thursday, October 30, 2014

SharePoint - Feature Stapling 2013

Here is the Code Code in Feature Stapling

A developer creates a Feature and to add it to every new Web site that is based on a specific site definition. The developer creates a feature-stapling Feature that includes mappings between the Feature and site definition

Technical Details

Feature stapling is implemented through a Feature that is specifically designed to staple other Features to one or more site definitions. Feature stapling allows a Feature to be stapled to any new sites created from any site definition or from specific site definitions based on the template name identified in the appropriate WEBTEMP.xml file.
Following is an example of feature stapling that associates the Feature with only the STS site definition templates.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#0" />
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#1" />
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#2" />
</Elements>
Following is an example of feature stapling that associates the Feature with all site definitions.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="GLOBAL" />
</Elements>

Support Details

Feature stapling starts only when the feature stapling entry is added. To add the Features to existing sites, or to sites later added through a restoration process, you must associate the Features with the sites by using some other method.
reference : http://msdn.microsoft.com/en-us/library/office/bb861862(v=office.12).aspx

Wednesday, October 29, 2014

Sharepoint 2010/2013 SPGridview Example Visual WebPart

Here is the DownLoad link SPGridView-Example
my Environment
-----------------
Visual Studio 2013
Sharepoint 2013

Custom List Name (PhotoList) and Column name below

----------------------------------------------------------------------
<table>
    <tr>
        <td>
            <SharePoint:SPGridView ID="gvICSSDocuments" runat="server" AutoGenerateColumns="false" Width="850px">
                <RowStyle BackColor="#D0D8E8" Height="30px" HorizontalAlign="Left" />
                <AlternatingRowStyle BackColor="#E9EDF4" Height="30px" HorizontalAlign="Left" />
                <HeaderStyle HorizontalAlign="Left" CssClass="ms-viewheadertr" />
                <Columns>
                    <asp:TemplateField HeaderText="Year" ControlStyle-Width="100px" SortExpression="Year" HeaderStyle-CssClass="ms-viewheadertr">
                        <ItemTemplate>
                            <asp:Label ID="lblCreated" runat="server" Text='<%# Eval("Year") %>'></asp:Label>
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title" ControlStyle-Width="250px" SortExpression="Title" HeaderStyle-CssClass="ms-viewheadertr">
                        <ItemTemplate>
                            <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" ControlStyle-Width="300px" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="ms-viewheadertr">
                        <ItemTemplate>
                            <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Attachment" ControlStyle-Width="200px" SortExpression="Name" HeaderStyle-CssClass="ms-viewheadertr">
                        <ItemTemplate>
                            <asp:HyperLink ID="hlnkDocument" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# Eval("Url") %>'
                                Target="_blank"></asp:HyperLink>
                        </ItemTemplate>

                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <asp:Label ID="lblNoAccess" Text="No documents available" runat="server" CssClass="emptyDataLabel"></asp:Label>
                </EmptyDataTemplate>
            </SharePoint:SPGridView>
        </td>
    </tr>
</table>
--------------------------------------------------------------
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
namespace SharePoint2013.LabExamples.SPGridViewWP
{
    public partial class SPGridViewWPUserControl : UserControl
    {
      

        ObjectDataSource gridDS = null;
        /// <summary>
        /// n the Page_Load event we are declaring the GridView Object, its event and Datasource
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                const string DATASOURCEID = "gridDS";
                gridDS = new ObjectDataSource();
                gridDS.ID = DATASOURCEID;

                gridDS.SelectMethod = "SelectData";
                gridDS.TypeName = this.GetType().AssemblyQualifiedName;
                gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);

                this.Controls.Add(gridDS);
                BindDocuments();

            }
            catch (Exception ex)
            {
                SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
                SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
                diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, ex.StackTrace, cat.Name, cat.Area.Name);
                SPUtility.TransferToErrorPage("Some Error occured, Please try after some time. <br/> If problem persists, contact your adminstrator");
            }

        }
        /// <summary>
        /// BindDocuments Method for Binding the objectDataSource to GridView
        /// </summary>
        private void BindDocuments()
        {
            gvICSSDocuments.AllowPaging = true;

            // Sorting
            gvICSSDocuments.AllowSorting = true;

            //allow Filtering
            gvICSSDocuments.FilterDataFields = "Year,Title,,Name";
            gvICSSDocuments.FilteredDataSourcePropertyName = "FilterExpression";
            gvICSSDocuments.FilteredDataSourcePropertyFormat = "{1} = '{0}'";
            gvICSSDocuments.Sorting += new GridViewSortEventHandler(gvDocuments_Sorting);
            gvICSSDocuments.PageIndexChanging += new GridViewPageEventHandler(gvDocuments_PageIndexChanging);
            //For Filtering
            gridDS.Filtering += new ObjectDataSourceFilteringEventHandler(gridDS_Filtering);
            gvICSSDocuments.AutoGenerateColumns = false;
            gvICSSDocuments.AllowFiltering = true;
            gvICSSDocuments.PagerTemplate = null;
            gvICSSDocuments.PageSize = 10;
            gvICSSDocuments.DataSourceID = gridDS.ID;       
            gvICSSDocuments.DataBind();
        }
        /// <summary>
        /// SelectData Method creates a DataTable from the data fetched from SharePoint List or Doc Library and returns the DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable SelectData()
        {
            DataTable dataSource = new DataTable();
            try
            {
                SPSite site = SPContext.Current.Web.Site;
                SPWeb web = site.OpenWeb();
                SPList lstICSSDocuments = web.Lists["PhotoList"]; //create document library or List and add column year,Description,Url,Name, Title is inbuilt.
                SPQuery query = new SPQuery();
                IEnumerable<SPListItem> lstItemICSSDocuments = lstICSSDocuments.GetItems(query).OfType<SPListItem>();

                dataSource.Columns.Add("Year");
                dataSource.Columns.Add("Description");
                dataSource.Columns.Add("Name");
                dataSource.Columns.Add("Title");
                dataSource.Columns.Add("Url");

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //SPGroup grpName = web.Groups["ICSSViewers"];
                    var committeeAndGroupDocumentswithURL = lstItemICSSDocuments.Select(x => new { Year = x["Year"], Description = x["Description"], Name = x["Name"], Title = x["Title"], Url = site.Url + "/" + x.Url });
                    foreach (var document in committeeAndGroupDocumentswithURL)
                    {
                        DataRow dr = dataSource.NewRow();
                        dr["Title"] = document.Title;
                        dr["Url"] = document.Url;
                        dr["Name"] = document.Name;
                        dr["Description"] = document.Description;
                        dr["Year"] = document.Year;
                        dataSource.Rows.Add(dr);
                    }

                });
                web.Dispose();
            }
            catch (Exception ex)
            {
                SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
                SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
                diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, ex.StackTrace, cat.Name, cat.Area.Name);
                SPUtility.TransferToErrorPage("Some Error occured, Please try after some time. <br/> If problem persists, contact your adminstrator");
            }
            return dataSource;
        }
        /// <summary>
        /// The Sorting, Filtering, PageChanging etc are all handled similar to normal asp.net gridviews
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = this;
        }
        void gvDocuments_Sorting(object sender, GridViewSortEventArgs e)
        {
            string lastExpression = "";
            if (ViewState["SortExpression"] != null)
                lastExpression = ViewState["SortExpression"].ToString();

            string lastDirection = "asc";
            if (ViewState["SortDirection"] != null)
                lastDirection = ViewState["SortDirection"].ToString();

            string newDirection = string.Empty;
            if (e.SortExpression == lastExpression)
            {
                e.SortDirection = (lastDirection == "asc") ? System.Web.UI.WebControls.SortDirection.Descending : System.Web.UI.WebControls.SortDirection.Ascending;

            }

            newDirection = (e.SortDirection == System.Web.UI.WebControls.SortDirection.Descending) ? "desc" : "asc";
            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = newDirection;

            gvICSSDocuments.DataBind();
            //For Filter
            if (ViewState["FilterExpression"] != null)
            {
                gridDS.FilterExpression = (string)ViewState["FilterExpression"];
            }

        }

        void gvDocuments_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvICSSDocuments.PageIndex = e.NewPageIndex;
            gvICSSDocuments.DataSourceID = gridDS.ID;
            gvICSSDocuments.DataBind();
        }
        //For Filtering
        private void gridDS_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
        {
            ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;
        }
        protected sealed override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            if (Context.Request.Form["__EVENTARGUMENT"] != null &&
                 Context.Request.Form["__EVENTARGUMENT"].EndsWith("__ClearFilter__"))
            {
                // Clear FilterExpression
                ViewState.Remove("FilterExpression");
            }
        }
    }

}

------------------------------------
After Deploy the webpart create one page and webpart see below the resule


-----------------
reference url :http://go4coding.com/post/2013/08/26/Using-Sharepoint-GridView-SPGridview.aspx