Wednesday, June 15, 2016

Adding HttpHandler to SharePoint


From Visua Studio SP Project -> Add Sharepoint Mapped Folder -> Layouts

From Visual Studio Add New Handler Called "MyHandler.ashx":

Inside MyHandler.ashx :

<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="MyTestProjectAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=af79cefbe6d4cf2c" %>
<%@ WebHandler Language="C#" Class="MyNameSpace.MyHandler" %>

-----
To Call you Handelr :
/_layouts/15/MyHandler.ashx



Thursday, June 2, 2016

Writing Code inside Sharepoint Global.ascx

I encounter situation that i need to add code inside sharepoint "Application_BeginRequest" Event ,
So to do that there is 2 ways :
   1- Using IHttpModule.
   2- Using Global.asax Event.

===============
1- Create New Class Library Project named for E.g "KNGenericHandlers":
================
A- First Using IHttpModule :

   A-1- Add Class named -> KNModule.cs

namespace KNGenericHandlers
{
    public class KNModule : IHttpModule
    {                
        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            //Your Code 
        }

        public void Dispose()
        {

        }
    }
}


  A-2- In Sharepoint Web.Config inside "<system.webServer>" -> "<modules runAllManagedModulesForAllRequests="true">"
       Add : <add name="KNModule" type="KNGenericHandlers.KNModule" />

================
B- Other Way Using IHttpModule :

     B-1- Add Class Global.cs:
---
namespace KNGenericHandlers
{
    public partial class Global : Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication
    {
        //KNGenericHandlers.Global
        public Global() { }
        protected void Application_EndRequest(Object sender, EventArgs e)
        {
            //ADD YOUR CODE
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
             //InitializeKentico Ini = new InitializeKentico();
        }
    }
}
---
 B-2-In your Sharepoint "global.asax" Edit the file to point to your class Library like :
---------
<%@ Assembly Name="KNGenericHandlers"%>
<%@ Import Namespace="KNGenericHandlers" %>
<%@ Application Language="C#" Inherits="KNGenericHandlers.Global" %>
---------
================