Friday, August 21, 2015

Step by step create provider hosted application using SP 2013

Hello Everyone,
As many of you are aware of newly introduced concept App model in SharePoint 2013.
For introduction and overview of SharePoint app model you can refer to this below article.
http://blogs.msdn.com/b/amigan/archive/2012/12/10/part-2-introduction-to-sharepoint-2013-app-model.aspx
In this article I will show development and deployment steps of one of the hosting models in SharePoint app model i.e. Cloud hosted SharePoint app.
Apps in SharePoint
SharePoint 2013 app model has two types of hosting model.
a. SharePoint Hosted Application.
b. Auto Hosted Application.
c. Provider Hosted Application.
Let us concentrate on the Provider Hosted Application. The remaining, I am planning to write a separate article.
Provider Hosted Application
Provider hosted apps are also a part of cloud hosting model, where app can be hosted on any remote web platform such as IIS server. And these apps are made available on deploying it on cloud i.e. using SharePoint online O365 site, or windows azure in case of Auto hosting.
It interacts with SharePoint 2013 by using one of the SharePoint client object models or its REST/OData-based web service. Depending on the scenario it gains authorization to SharePoint data using either the SharePoint 2013 implementation of the OAuth standard or a JavaScript cross-domain library.
Here is how to create a basic provider-hosted app for SharePoint with the Office Developer Tools for Visual Studio 2012, how to interact with SharePoint 2013 sites by using the SharePoint CSOM, and how to implement OAuth in an app for SharePoint.
Prerequisite for building provider hosted app.
  • ·         Visual Studio 2012 installed on your developer computer.
  • ·         Office Developer Tools for Visual Studio 2012.
Microsoft Office Developer Tools for Visual Studio 2012 - RTM

A provider-hosted app for SharePoint consists of both an app for SharePoint that is deployed directly to a SharePoint 2013 site and a separately deployed web application. If the provider-hosted web application is an ASP.NET web application, you can use the Office Developer Tools for Visual Studio 2012 to create both components of a provider-hosted app for SharePoint.

1)      Open the Visual Studio 2012 as Administrator

2)      Click New Project.

3)      Select the Template App for SharePoint 2013. Name the Project as SampleProderApp.
  4)      On the Creation of the Solution, Visual Studio will ask for the Site Collection against which we are going to deploy our app. And on the same screen, we need to choose the type of hosting which we are planning. In our case, it is going to be Provider Hosted Application.
 5)       On the selection of Provider Hosted, Click Next. The below screen will be asking the Authentication setting.
 
Configure SharePoint for low-trust Apps using ACS.
Low-trust Apps are those that use an authentication provider to act as a common authentication broker or trust broker between SharePoint and the App.  This will be Windows Azure Access Control Services (ACS), and as such will of course require an Office 365 subscription.
ACS is where SharePoint requests a context token that it can send to the location hosting the App. The App then uses the context token to request an access token from ACS. Once received, this is used by the App to talk back to SharePoint. Y ou can read more about the detail of the OAuth flow for low-trust Apps here:  OAuth authentication and authorization flow for cloud-hosted apps in SharePoint 2013.

6)      Now, the Solution has been created. The basic solution is as follows.
 

7)       Our solution will comprise of 2 projects.
a. App Project: This is going to be deployed on the SharePoint.
b. AppWeb Project : This is going to be the .Net Web Application. This application can be hosted on any IIS.
8)      I am using the IIS Express to host the application.
9)      Go to the AppManifest.xml in the App project. AppManifest file will be looking like this.
 
When you are using Visual Studio and the tools, you can use this (~remoteAppUrl) token for any provider-hosted app and it is resolved when Visual Studio packages the app.

10)   Go to Permissions Tab. Add the corresponding scope and the permissions as per the requirement.
 

11)   Now, we came to the final step of our provider hosted application creation. That is, we need to register our clientID with the SharePoint before deploying the app.
12)   ClientID is nothing but a GUID. But the only special thing about the ClientID is, it should be small case. i.e., something like 24576c92-961f-442b-a866-e612222cad36
13)   From our SharePoint site itself, go to the AppRegNew.aspx page. The page will present inside the layouts folder. The URL would be something like https://givemedomain.sharepoint.com/sites/Dev/_layouts/15/appregnew.aspx
14)   This entry in Web.config file shows ClientSecret and client id.
 <appSettings>
    <add key="ClientId" value="" />
    <add key="ClientSecret" value="NnEHNtSptui3ZF/ijWargTad5M26WKlx6mBvKcD55vQ=" />
  </appSettings>
The identity of an app, and its client secret, are stored in the Windows Azure Access Control Service (ACS), and the ACS is the authorization authority. When a user first grants an app permissions to access SharePoint 2013 resources on the user's behalf, SharePoint 2013 obtains information about the app from ACS. SharePoint 2013 then stores the basic information about the app in the app management shared service. The content database service and other components, such as the user profile service, can get the display name of an app and other basic information about the app directly from the app management shared service.
15)   Press F5 to deploy this app on the developer site.
16)   Login to the developer site and see in app catalog, this app will be hosted.
17)   Now you can refer the app from any pages from SharePoint farm.

Friday, August 14, 2015

Create News Ticker in SharePoint 2013 Using jQuery

This article explains how to create a SharePoint 2013 News Ticker using JQuery. Look at the following procedure.

Step 1
Open Visual Studio.

visual studio

Step 2
Create a new project and select SharePoint 2013 Empty project.

SharePoint 2013 Empty project

Step 3
Deploy as a Farm Solution.

Deploy as a Farm Solution

Step 4
Add a new item into the empty SharePoint project.

add new item

Step 5
Create a new Visual webpart.

Step 6
Now add the jQuery references into the SharePoint LAYOUTS Folder.

SharePoint LAYOUTS Folder

Step 7
Add a reference into the page "Ticker.ascx".

Design
Just add a ticker activities into it.
  1. < script > $(document).ready(function() {  
  2.     $('#NewsTicker').vTicker({  
  3.         speed: 500,  
  4.         /*Speed of the feed message*/  
  5.         pause: 3000,  
  6.         /*Pause time*/  
  7.         showItems: 1,  
  8.         /*No of News to display*/  
  9.         animation: 'fade',  
  10.         /*animation styles*/  
  11.         mousePause: true,  
  12.         /* pause when hover */  
  13.         direction: 'up' /*Text direction*/  
  14.     });  
  15. }); < /script>  
Add a simple style for the NewsTicker
  1. <style type="text/css" media="all">  
  2.  
  3.     #NewsTicker  
  4.     {  
  5.         width: 844px;   
  6.         margin: auto;  
  7.     }  
  8.  
  9.     #NewsTicker ul li div  
  10.     {  
  11.         height:30px;  
  12.         background: Yellow;  
  13.     }  
  14.   
  15.     <div style="width:1310px; height:30px; border-style:solid; border-width:2px; border-color:#FFCB05">  
  16.         <div style="float:left;background-color:White; height: 27px; width: 118px;">  
  17.             <h2 style="font-family:Arial; font-size:22px; background-color:#FFCB05; color:Black;">News</h2>  
  18.         </div>  
  19.         <div id="NewsTicker" style="float:left; padding-left:15px; font-size:24px; font-family:Arial; height: 29px;">  
  20.             <ul style="width: 920px">  
  21.                 <asp:Literal ID="ltNews" runat="server" Text=""></asp:Literal>  
  22.             </ul>  
  23.         </div>  
  24.     </div>  
  25. </style>  
Now to bind the list
Add these two namespaces.
  1. using System.Text;  
  2. using Microsoft.SharePoint;  
Code
  1. private void BindData()   
  2. {  
  3.     Guid siteId = SPContext.Current.Site.ID;  
  4.     Guid webId = SPContext.Current.Web.ID;  
  5.     StringBuilder sb = new StringBuilder();  
  6.     SPSecurity.RunWithElevatedPrivileges(delegate   
  7.     {  
  8.         using(SPSite site = new SPSite(siteId))   
  9.         {  
  10.             using(SPWeb web = site.OpenWeb(webId))   
  11.             {  
  12.                 SPList list = web.Lists.TryGetList("News"); /*Create the list*/  
  13.                 if (list != null)   
  14.                 {  
  15.                     foreach(SPListItem item in list.Items)   
  16.                     {  
  17.                         sb.AppendLine("<li>");  
  18.                         sb.AppendLine("<div>");  
  19.                         sb.AppendLine(item.Title); /*Get the title column*/  
  20.                         sb.AppendLine("</div>");  
  21.                         sb.AppendLine("</li>");  
  22.                     }  
  23.                 }  
  24.   
  25.             }  
  26.         }  
  27.     });  
  28.     ltNews.Text = sb.ToString();  
  29. }  
Step 8
Now save and deploy.

solutions 

Step 9
So the solutions have been deployed now.

Go to the site.

Here I am have created a list named “News”.

News

Step 10
The news lists have two feeds.

news lists

Step 11
Add a Newsfeed webpart from the “Custom” tab.

Custom

Add a Newsfeed

webpart

webpart

So we can use this newsfeed dynamically.

Document Conversion Service in SharePoint 2013

This service helps to convert documents into web pages. This article explains how to configure a document conversion service in SharePoint and how to use it.

Open Central Administration.

Open Central administration

Go to "System Settings" and click "Manage services on server".

Click manage services on the server

Let's start the following two services.

start this two service

Click on Document Conversions Load Balancer Service, then click Start.

Load Balancer Service

Now click on Document Conversions Launcher Service.

Select your "Load Balanced server".
Load balanced server

Click OK.

Click Ok

Now this service has been started successfully.

Click on "General Application Settings" in "Central Administration".

General application settings

Click "Configure document conversions".

Click on configure document Conversions

Now it will look like this.

Now it look

Select your web application.

webapplication

Enable document conversions for this site and click Yes.

Select "Load Balanced server".

Select Load balanced server

Click Apply, then click OK to complete this configuration.

Open the site and create the Document Library.

Create document library

Upload the Word document now.

choose file

Here you can see the converted document to the webpage option.

Upload the word document

Click on it.

convert document to webpage

Set the title for the page.

Click on it

Click Create.

Set the title for the page

output

Wednesday, August 12, 2015

Programmatically get/search SharePoint List items using SPView, SPList.GetItems (SPView)

using (SPSite site = SPContext.Current.Site)
{
  if (site != null)
  {
     using (SPWeb web = site.OpenWeb())
     {
        SPList list = web.Lists["your list name"];

        if (list != null)
        {
            foreach (SPListItem item in list.Items)
            {
            //do something here
            //If you are using ascx page for webpart
            //or want to display on aspx page
           
            Response.Write(item.Title.ToString());

            }
        }
        web.Close();
     }
     site.Close();
   }
}

----------------------------------
SPList.GetItems Method (Microsoft. SharePoint)
Returns a collection of items from the list.
Overload List
Name
Description
SPList.GetItems (SPQuery)
Returns a collection of items from the list based on the specified query.
SPList.GetItems (SPView)
Returns a collection of list items from the list based on the specified view.
SPList.GetItems (SPQuery, String)
Returns a collection of list items from the list based on the specified query and view.
Because SharePoint views (SPView) don't contain definition for SPListItems or SPListItemCollections, we can programmatically retrieve list items from a list in the criteria specified by a view you've created. Items received this way also preserves sorting.
                We do the trick using following code:
SPListItemCollection coll = web.Lists["ListName"].GetItems(web.Lists["ListName"].Views["ViewName"]);
                Complete code block:
using (SPSite site = new SPSite("http://stec/testweb"))
{
    if (site != null)
    {
        using (SPWeb web = site.OpenWeb())
        {

            SPList list = web.Lists["ListName"];

            if (list != null)
            {
                SPListItemCollection coll = web.Lists["ListName"].GetItems(web.Lists["ListName"].Views["ViewName"]);

                foreach (SPListItem item in coll)
                {
                    //do something
                    //if you are testing on console application
                    Console.WriteLine(item.Title.ToString());
                }
            }
            web.Close();
        }
        site.Close();
    }
}

[where "ListName" and "ViewName" are the names of your lists and views, respectively.]