Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Sunday, September 8, 2013

Custom Document Library Viewer SharePoint 2013 part 1

Lately I came across an interesting task and was able to successfully deliver it. I know that we have a Custom Library Viewer in SharePoint OOTB. I had a different requirement from the one already present in the SharePoint. I had been asked to develop a Custom Document Library Viewer based on Content Types and also display each of the content type in a separate tab.

So, the requirement was something like that for instance in a document library I have 4 different kinds of content types and there are several items in it. I will have to show 4 different tabs and also display the items in each tab using a grid. Interesting isn't it?!

Yea so this gave birth to a new generic web part based on jQuery tabs, and jQGrid. Which is configurable to use any document library, hosted on any website and user can choose which content types to show for a particular library!

Genius isn't it, alright so stay tuned I will be sharing the implementation details in the next post.

CORS support for ASP.NET Web API


CORS (Cross-Origin Resource Sharing) in an ASP.NET Web API 


I have been facing a dreadful issue "access denied access-control-allow-origin not included in response" error whenever I tried to send an AJAX request. I was able to land my call to the web API service and also checked that the Web API service was returning the correct response with correct headers. Still I was unable to find the solution to it and it really got frustrating.

I followed several tutorials on the web to allow the CORS in web API like:

http://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API

then I found this one

http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx

This tutorial here was the most comprehensive one and actually contained everything that we should know about CORS and moved me one step ahead. I was successfully able to send CORS requests on my local machine.

Again this error appeared when I deployed in production and then I had to remove the previous CORS handler. I was back to square one and was really desperate to find a solution because this was happening in production.

Finally, I found the magical lines which did the trick for me. Thanks to http://encosia.com/using-cors-to-access-asp-net-services-across-domains/ article.

<system.webServer>
 <httpProtocol>
  <customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
 </httpProtocol>
</system.webServer>


I hope it helps someone in need because I definitely know how miserable our life gets if such an issue appears.

Tuesday, July 30, 2013

Ajax call to Page Methods

Folks,

Recently I got to work on a .NET based Security Token Service and faced an issue where I needed to call a web method from with in the page without creating a new session, normally whenever we use ajax call we get a new session which would redirect us back to the login page of the Application as it doesn't know whether the call was from the authenticated user or what.

Anyway, so I hope you all understand the issue. The main purpose of the function was to get the data from the session for a particular key, I know first thing that comes to your mind would be that I must have used

<%=Session['key']%>

Which is what we use all the time but no this time the scenario was different. I had to get the value because I had an Iframe in the page which was changing the value of that particular key. So what I did was, I googled a bit and got to know about PageMethods. So here it is how I used it


Paste this in your form tag:



<asp:ScriptManager ID="ScriptManagerMain" runat="server" EnablePageMethods="true"
        ScriptMode="Release" LoadScriptsBeforeUI="true">
    </asp:ScriptManager>

And use the javascript:

  function onSuccess(data) {
            window.location = data;
        }
        function onFailure(error) {
            window.location = 'where-ever-I-wanted-it-to-go';
        }
        function GetCurrentSessionValue() {
            // call server side method
            PageMethods.GetSessionValue(onSuccess, onFailure);
        }

This is my Code Behind where I wrote this method.

 [WebMethod]
        public static string GetSessionValue()
        {
           return HttpContext.Current.Session["RedirectUrl"].ToString();
        }


I hope it helps. For any query just post a comment and I will get back to you.