About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Sunday, 6 October 2013

How to delete SharePoint event receivers on feature deactivating method.


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            SPWeb web = properties.Feature.Parent as SPWeb;
            try
            {

             SPList list = web.Lists.TryGetList("List name"); 

               if (list != null)
                 {
                  SPEventReceiverDefinition receiverDefinition = null; 

                  for (int i = list.EventReceivers.Count - 1; i >= 0; i--)
                            {
                                receiverDefinition = list.EventReceivers[i];
 
                                receiverDefinition.Delete();
                            }
                        }
            }

            catch (Exception ex)
            {

                PortalLog.LogString("Exception - {0}  -- {1} -- {2}", "XXXXXX", ex.Message, ex.StackTrace);

            }
        }

Auto increment SharePoint 2010/2007/2013 List column


It is very simple to increment column automatically. There are many ways of doing so,
1. SharePoint designer workflow
2. Calculated columns
3. SharePoint Visual studio workflows
4. Event receivers.
Out of all, option 4 is best feasible for all scenarios. I am not discussing disadvantages of other approaches. But SharePoint item event receives are very powerful in a multi threading (concurrency) scenarios than all other options.
1.  Get the top value from the list. Put following code in method 

private int GetAutoIncrementNumber(SPItemEventProperties properties)

{
int autoNumber = 0;
SPQuery query = new SPQuery();

           query.RowLimit = 1;

           query.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Auto_Increment_Number' />";

           query.Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>"; 

           SPListItemCollection items = properties.List.GetItems(query); 

           if (items.Count > 0)
           {

               string tmpNumber = string.Empty; 

               if (items[1]["Auto_Increment_Number"] != null)
               {

                   tmpNumber = items[1]["Auto_Increment_Number"].ToString();

                   int validWPDNumber;                 

                   autoNumber = Convert.ToInt32(tmpNumber);

               }         

}
return autoNumber;
}
Call the method in list item event receiver and get the highest value and increment accordingly. It’s very simple and for me working like charm.

Application page as pop up on event click in SharePoint 2010


In this example I will show you how to open application page as a pop up using java script. It is very commonly and most important requirement in SharePoint 2010 development.

Here is an example, say I have an application page called applicationpage1.aspx and code behind is - applicationpage1.aspx.cs. And want to open the application page as pop up on click of hyperlink.
Write a java script code in applicationpage1.aspx as follows, in this example, I am passing only one parameter.
Briefly
Parent page - The java script code in .aspx page
<script type="text/javascript">
function EditDoc(itemID) {
        var options = {
// path of the document to be opened as pop up. Replace ‘xxxxxx’ with you actual page.          

url: "/_layouts/XXXXXXXX/EditDocument.aspx?ID=" + itemID, allowMaximize: true, showClose: true, height: 300, dialogReturnValueCallback: function (result, returnValue) {
                window.location.href = window.location.href;
            }
        };
        modalDialog = SP.UI.ModalDialog.showModalDialog(options);
    }
</script>
Parent page – The item databound of the Telerik grid or data grid in code behind page
(dataItem["EditDoc"].FindControl("EditDocument") as HyperLink).NavigateUrl = string.Format("javascript:EditDoc({0});", dataItem["ID"].Text);
In child page (opened as pop up) auto refresh back to parent page
After you logic, at the end use following code
Context.Response.Write(string.Format("<script type='text/javascript'>window.frameElement.commitPopup();</script>"));

            Context.Response.Flush();

            Context.Response.End();
In detail
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/init.js"></script>
<script type="text/javascript" src="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/core.js"></script>
<script type="text/javascript">
function EditDoc(itemID) {
        var options = {
// path of the document to be opened as pop up. Replace ‘xxxxxx’ with you actual page.           
url: "/_layouts/XXXXXXXX/EditDocument.aspx?ID=" + itemID, allowMaximize: true, showClose: true, height: 300, dialogReturnValueCallback: function (result, returnValue) {
                window.location.href = window.location.href;
            }
        };
        modalDialog = SP.UI.ModalDialog.showModalDialog(options);
    }
</script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<telerik:RadGrid ID="RadGrid1" AllowPaging="false" runat="server" AllowSorting="true"
 
            AutoGenerateColumns="false" AllowFilteringByColumn="false" HeaderStyle-Font-Bold="true"

            OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound" GridLines="None" OnItemCommand="RadGrid1_ItemCommand" CssClass="OverrideTelerikGridStyles">
        <ClientSettings EnableAlternatingItems="false"></ClientSettings>
        <MasterTableView GroupLoadMode="Client" TableLayout="Fixed" GroupsDefaultExpanded="true">
        <Columns> 

<telerik:GridTemplateColumn ItemStyle-Width="110px" UniqueName="EditDoc" AllowFiltering="false" ItemStyle-ForeColor="Blue" ItemStyle-Font-Underline="true">             

                    <ItemTemplate>   

                        <asp:HyperLink ID="EditDocument" runat="server" Text="Edit Docuemnt"></asp:HyperLink>                    

                    </ItemTemplate>
         </telerik:GridTemplateColumn>
</Columns>
        </MasterTableView>    

    </telerik:RadGrid> 
</asp:Content>
In applicationpage1.aspx.cs
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)

        {

            try

            {

                if (e.Item is GridDataItem)

                {

                    GridDataItem dataItem = e.Item as GridDataItem;

(dataItem["EditDoc"].FindControl("EditDocument") as HyperLink).NavigateUrl = string.Format("javascript:EditDoc({0});", dataItem["ID"].Text);
}
}
}

Second part… EditDocument.aspx (this is the page opened as pop up) now once you are done entering some data and want to save and go back to the parent page. How to go back to parent page without refreshing the parent page
Say I have two buttons in pop page say OK and Cancel.
Following code in OK button click event
Context.Response.Write(string.Format("<script type='text/javascript'>window.frameElement.commitPopup();</script>"));

                Context.Response.Flush();
                Context.Response.End();
Following code in Cancel button click event
Context.Response.Write(string.Format("<script type='text/javascript'>window.frameElement.commitPopup();</script>"));

            Context.Response.Flush();
            Context.Response.End();

Happy coding....