Saturday, September 14, 2013

Suppress Javascript Errors

There are times when we see, due to any reason, our javascript in the page starts throwing errors. Hence if some one is browsing the page using Internet Explorer, they might come across the annoying popups if they have configured the Internet settings like that, this is where this Tutorial might come in handy for a few coders like me tackling the situation.

Again it's not a solution it's just a way to handle and tackle the issues, I would still say if you have time in your hand go through your javascript and get it fixed. This snippet must be used if and only if, you are out of time and options.



I hope it gives peace to someone in need!


Wednesday, September 11, 2013

SharePoint 2014 Conference

Dear All,

Microsoft SharePoint conference is back! You can get the details to register on the following link Details

SharePoint Conference offers a wide variety of presentations, labs, certification testing, Q&A sessions, workshops, and networking events for attendees to build their skills and meet other SharePoint professionals. Along with the best new content, industry trends, and product announcements, SharePoint product managers, engineers, and other experts will be answering questions, giving presentations, and hosting discussions

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.

Thursday, March 7, 2013

Change the default login page of SharePoint 2013 web application

I had to change the default login page for SharePoint 2013 web application because I am using a Claim based authentication for my web application with a custom login page.

It looks really simple but it really helped me a lot for the customization.

Set-SPCustomLayoutsPage -Identity "Login" -RelaivePath "/_layouts/custompages/customLogin.aspx" -WebApplication "{replace with web app url}"

I hope it helps.

Thursday, January 24, 2013

SHAREPOINT CUSTOM SEARCH WITH KENDOUI


As discussed in the previous post I had planned to incorporate Kendo UI with the SharePoint 2013 Custom Search Results page, I am going to develop a Web Part which will use the Kendo Grid.

For those who are still wondering what this kendo UI is all about.. It really is a cool; HTML5, Javascript etc based Framework which provides several widgets for more details visit <a href="http://www.kendoui.com/">http://www.kendoui.com/</a>

INTEGRATING KENDO UI
In the previous section we made a user control now user control has both .ascx page and the code behind file. Add the reference to the kendo UI style sheets but before doing that we need to place the files in the 15hive in the Layouts folder so that these style sheets are available for our user control. Place them in the STYLES folder in the Layouts folder.


Add Script tag in the file to use kendoUI:
  <link href="/_layouts/15/kendoFiles/Content/kendo/2012.2.710/kendo.common.min.css" rel="stylesheet" type="text/css"></link>
    <link href="/_layouts/15/kendoFiles/Content/kendo/2012.2.710/kendo.blueopal.min.css" rel="stylesheet" type="text/css"></link>
  <script src="/_layouts/15/kendoFiles/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="/_layouts/15/kendoFiles/Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
<script src="/_layouts/15/kendoFiles/Scripts/kendo/2012.2.710/kendo.web.min.js" type="text/javascript"></script>
    <script src="/_layouts/15/kendoFiles/Scripts/kendoui-Custom.js"></script>
   
   <script type="text/javascript">
       function getParameterByName(name) {
           var match = RegExp('[?&]' + name + '=([^&]*)')
                           .exec(window.location.search);
           return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
       }
       var queryTerms = getParameterByName('k');//for the case if there is query string search request
       function PerformSearch() {
           queryTerms = document.getElementById('query').value;
           Results.init($('#resultsDiv'));
           Results.load();
         
       }      
       Results = {
           element: '', url: '',
           init: function (element) {
               Results.element = element;
               Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryTerms + "'";
           },
           load: function () {
               $.ajax({
                   url: Results.url,
                   method: "GET",
                   headers: { "Accept": "application/json; odata=verbose" },
                   success: Results.onSuccess, error: Results.onError
               });
           },     onSuccess: function (data) {
               var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
               var dSource = new Array();            
               var html = "<table>";
               for (var i = 0; i < results.length; i++) {
                   dSource[i] = new Object();
                   dSource[i].Title = results[i].Cells.results[3].Value;
                   dSource[i].Path = results[i].Cells.results[6].Value;
               }
               $("#products").kendoGrid({
                   dataSource: dSource                
               });

           },
           onError: function (err) { alert(JSON.stringify(err)); }
       }

   </script>

In this JavaScript snippet we are creating a KendoUI Grid in the div that we have allocated for it in the .ascx file(as shown below). We will use the OData Services exposed by the SharePoint 2013 to call the search service and use the data returned by the service to create a data source for our KendoGrid control. And this is how we will show the results.

Now the Kendo UI and the Custom Search is all setup, enter any search query on our visual webpart and you will get the result, currently it is showing only two search result fields which we will alter using the column model of KendoGrid.






With the help of the latest REST based services provided by the SharePoint using kendo Grid or any other ajax supported grid has become real easy.

I will share further posts related to SharePoint 2013 development or ASP.Net which could be of any help to the developers who are seeking help in this regard.

Wednesday, January 16, 2013

SharePoint 2013 Custom Search Result using Visual Studio 2012


We will have our own custom Search results page which will consist of a visual web part containing a User Control.

A.1.        Search Mechanism

Once the SharePoint Search Service is running and has something to crawl, the user will be able to search through the content of the SharePoint website, lists, document libraries and pages. Our search Web part will be shown instead of the OOTB search bar of SharePoint. Upon clicking the search button user will be redirected to the custom search results page. Following query string values are also sent while redirecting to this page which we will use these query strings to call the search on our custom search page.

A.2.        Custom Search Page

This page will have a Visual Web Part containing a User Control containing the Grid view to show the results of the search. The User control will use the REST based service exposed by the SharePoint. The details of the API are as follows:

A.2.a.     Location of the Search Rest service

                The Search REST service is located at the following URI: http://host/site/_api/search


A.3.        Visual Web Part Implementation

 

Go to the file-> Add new project and then Select Empty SharePoint Project. A new solution will be created now right click the project file and add new Visual Web Part to the project.

Your project will look like just as shown in the picture above, now add the following code to the .ascx file in your project.



<%@ Assembly Name="$SharePoint.Project.AssemblyFullNamequot; %>


<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>


<%@ Import Namespace="Microsoft.SharePoint" %>


<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1.ascx.cs" Inherits="searchWebPart.VisualWebPart1.VisualWebPart1" %>


Custom Result Page


Search: <input id="query" type="text" /> <input id="searchBtn" type="button" onclick="PerformSearch()" value="button" />


<%-- <asp:Button ID="searchBtn" runat="server" Text="Button" OnClientClick="PerformSearch()" CommandName="test" />--%>


<div id="resultsDiv" style="display:none;"></div>


<div id="products"></div>



Once you have setup the files as mentioned above, deploy the project using visual studio 2012 and it will place the User control in the Visual Web Part in the ControlTemplates folder of the 15 hive. So your web part will appear in the Custom Web Parts category and can be used on any page.

Once the Web Part has been deployed successfully, create a new site page in the site library and add this WebPart to the page. Stay tuned for the next version of this post where I shall show how to use Kendo UI to display the Search Results.

Tuesday, January 15, 2013

Welcome Developers

Impressed by the numerous blogs present on the web to help us (the developers) on day to day issues that we face while developing various applications, I have decided to create a blog. So far I have decided to start helping people and for that reason I am going to kick off with this Blog.