Tag: SharePoint 2013

Provider Hosted App For SharePoint Online

Create a new project in visual studio using “App for SharePoint” template, here I’m using visual studio 2015 version. After selected project can see a wizard

2016-01-24_10-20-52

Enter SharePoint online URL and select “Provider-Hosted” option next it will ask for user name and password, then wizard will automatically select “SharePoint Online” Option

2016-01-24_10-26-11

Next select ASP.Net MVC Web Application in the wizard

2016-01-24_10-29-44

Next leave by default selection of “Use Windows Azure Access Control Service (for SharePoint Cloud Apps)” then finish the wizard.

Create new razor view under home view folder

2016-01-24_12-02-36

Add new ActionResult in HomeController.cs, below code will retrieve list items from the “Shared Documents” and generates a DataTable. pass the DataTable to View.

 public ActionResult DocumentList()
{
DataTable dt = new DataTable();
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientcontext = spContext.CreateUserClientContextForSPHost())
{
List list = clientcontext.Web.GetList(spContext.SPHostUrl.ToString() + "Shared Documents");
clientcontext.Load(list);
CamlQuery cq = new CamlQuery();
ListItemCollection lic = list.GetItems(cq);
clientcontext.Load(lic, items => items.Take(15).Include(item => item["FileLeafRef"], item => item["Modified"],
item => item["Author"], item => item["FileRef"], item => item["File_x0020_Size"]));
clientcontext.ExecuteQuery();
dt.Columns.Add("File Name");
dt.Columns.Add("Modified", typeof(DateTime));
dt.Columns.Add("Author");
dt.Columns.Add("Size");

foreach (ListItem item in lic)
{
DataRow dr = dt.NewRow();
dr["File Name"] = "<a href=\"" + spContext.SPHostUrl.ToString().TrimEnd('/') + item["FileRef"]
+ "\">" + item["FileLeafRef"] + "</a>";
dr["Modified"] = item["Modified"];
dr["Author"] = ((FieldUserValue)item["Author"]).LookupValue;
dr["Size"] = item["File_x0020_Size"];
dt.Rows.Add(dr);
}
}

return View(dt);
}

Add below code to your new view, code will generate a HTML table using our ModelTable.

<div style="padding:5px 10px 3px 5px">
<table border="1" cellpadding="5" id="my-table">
<thead>
<tr>
@foreach (System.Data.DataColumn col in Model.Columns)
{
<th>@col.Caption</th>
}</tr>
</thead>
<tbody>
@foreach (System.Data.DataRow row in Model.Rows)
{
<tr>
@foreach (var cell in row.ItemArray)
{
<td>@Html.Raw(cell.ToString())</td>
}</tr>
}</tbody>
</table>
</div>

View name and action result name should be same else mention the view name in the return statement of action result.

In the AppManifest file select the permissions tab and Scope as list and permission as Read

2016-01-24_14-10-05

Run the project in visual studio using play button (F5), now can see SharePoint App Permission approval page, select Documents list then click “Trust it”

2016-01-24_14-13-02

After that SharePoint will redirect to our local hosted site, navigate to newly created view page. Page will display SharePoint list data.

2016-01-24_14-40-48

You can now compare your retrieved data with the actual SharePoint list.

2016-01-24_14-41-22

JQUERY BASICS – Part 2

load()
Using load method we can load other page’s full content or other page’s particular control inner HTML
Ex. Below code will load the MainDiv inner HTML. so inside the load method we need to pass page URL as a parameter if you want load full other source page or if you want to load particular element’s inner HTML then after the URL parameter give one space then enter the selector, here I’ve passed div id as selector (#MainDiv)

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>main page</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<b>HTML from another page:</b>

<div id="new-projects"></div>

 
<script>
 $("#new-projects").load("anotherpage.html #MainDiv");
</script>

</body>
</html>

This is the other source page

 

 
<!doctype html>
<html lang="en">
<head>
<title>another page</title>
</head>
<body>
full HTML contents From another page
<div id="MainDiv">
<h3 style="color:green">div inner html from another page</h3>
</div>
</body>
</html>

OutPut

2016-01-16_16-33-35

each()

each() will take collection from selector and loop the items, this is same as Foreach in c#.
Ex. Below code will loop the all div elements and elements inner HTML append into one p tag.

 
<!DOCTYPE html>
<html>
<head>
<title>For Each</title>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
 <script>
 $(document).ready(function () { $('div').each(function () { $('.ptag').append($(this).html()); }); });

 </script>
</head>
<body>
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<p class="ptag"></p>


</body>
</html>

Output

2016-01-16_16-34-27

slideToggle()

The jQuery library provides several techniques for adding animation to a web page, slideToggle will hide and show the targeted element,

Ex, below code will hide and show the all div elements

</pre>
<!DOCTYPE html>
<html>
<head>
<title>Show Hide</title>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div>Div 1</div>


<div>Div 2</div>


<div>Div 3</div>


<div>Div 4</div>

 <input type="button" id="button" value="click here" />
 <script>
 $('#button').click(function () { $('div').slideToggle(1500); });
 </script>
</body>
</html>

Retrieve List data in SharePoint Online (office 365) using JSOM

Create new project using App for SharePoint template under Apps in Visual Studio.

step1

In the new project wizard enter your SharePoint site URL and select SharePoint Hosted and click Finish.

step1a

After project created open solution explorer and select App.js file for edit

step2

var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var listItems;
var web;

$(document).ready(function () {
 //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
 getUrl();
});

function getUrl() {
 hostweburl = getQueryStringParameter("SPHostUrl");
 appweburl = getQueryStringParameter("SPAppWebUrl");
 hostweburl = decodeURIComponent(hostweburl);
 appweburl = decodeURIComponent(appweburl);
 var scriptbase = hostweburl + "/_layouts/15/";
 $.getScript(scriptbase + "SP.Runtime.js",
 function () {
 $.getScript(scriptbase + "SP.js",
 function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
 );
 }
 );
 event.preventDefault();
}

function execOperation() {
 context = new SP.ClientContext(appweburl);
 var factory =
 new SP.ProxyWebRequestExecutorFactory(
 appweburl
 );
 context.set_webRequestExecutorFactory(factory);
 appContextSite = new SP.AppContextSite(context, hostweburl);
 web = appContextSite.get_web();
 context.load(web);
 var camlQuery = new SP.CamlQuery();
 camlQuery.set_viewXml("");
 list = web.get_lists().getByTitle("Documents");
 listItems= list.getItems(camlQuery);
 context.load(list);
 context.load(listItems);
 context.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
 $("#message").empty();
 var listInfo = '';
 var listEnumerator = listItems.getEnumerator();

 listInfo += "
<table>
<tr>
<th>Id</th>
<th>Title</th>
</tr>

";

 while (listEnumerator.moveNext()) {
 var listItem = listEnumerator.get_current();
 listInfo += '
<tr>
<td>' + listItem.get_item('ID') + '</td>

'
 + '
<td>' + listItem.get_item('FileLeafRef') + '</td>

'
 + '</tr>

\n';
 }

 listInfo += '</table>

';

 $("#message").html(listInfo);
}

// This function is executed if the above call fails
function onFail(sender, args) {
 alert(args.get_message());
}
function getQueryStringParameter(paramToRetrieve) {
 var params =
 document.URL.split("?")[1].split("&");
 for (var i = 0; i < params.length; i = i + 1) {
 var singleParam = params[i].split("=");
 if (singleParam[0] == paramToRetrieve)
 return singleParam[1];
 }
}

Code flow

Step 1: getUrl() function called when page get ready

Step 2: getUrl() function is collect host and app web URLs from query strings and adds some JavaScript files for reference and call execOperation() function

Step 3: execOperation() function is create app sire context and request Documents list data as asynchronous call

Step 4: onSuccess() event will receive list data, data written as html table and table added into div control

Document library in SharePoint online
Document library in SharePoint online
App view
App view

Continue reading “Retrieve List data in SharePoint Online (office 365) using JSOM”