SharePoint Lists with CSOM APIs using Provider hosted app.
1. Microsoft visual studio 2013 -> file -> project -> "MyCSOMApp"
5. add 'AppIcon.gif' under 'img' folder.
6. open 'App.css' file and add below CSS.
7. open 'Pages" -> 'Default.aspx' page and add below code.
9. go to "MyCSOMApp" -> "AppManifest.xml" -> open "Permission" tab.
10. perss 'F5' to deply and test.
1. Microsoft visual studio 2013 -> file -> project -> "MyCSOMApp"
2. add "Content" Folder under "MyCSOMAppWeb" directory.
3. add 'App.css' file under "Content" Folder.
4. add 'img' folder under 'Content' folder.5. add 'AppIcon.gif' under 'img' folder.
6. open 'App.css' file and add below CSS.
body {
padding: 0px;
margin: 0px;
background-color: orange;
}
#page_width {
width: 960px;
margin: auto;
background-color: white;
border: 1px solid black;
}
#nav_bar {
padding: 4px;
background-color: black;
}
#nav_bar a {
font-size: 12px;
text-decoration: none;
color:white;
}
#page_header {
height: 72px;
}
#site_logo {
float: left;
background-image: url("img/AppIcon.gif");
width: 75px;
height: 75px;
}
#site_title {
margin: 0px;
margin-top: 12px;
float: left;
font-size: 40px;
color: #000;
}
#toolbar {
margin: 0px;
padding: 6px;
background-color: #CCC;
border-top: 1px solid black;
border-bottom: 1px solid black;
}
#content_box {
margin: 16px;
padding: 8px;
background-color: #EEE;
border: 1px solid #CCC;
border-radius: 8px;
min-height: 480px;
font-size: 1.35em;
color: darkblue;
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyCSOMAppWeb.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head runat="server">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<title>My CSOM App</title>
<link href="../Content/App.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div id="page_width">
<div id="nav_bar">
<asp:HyperLink ID="HostWebLink" runat="server" />
</div>
<header id="page_header">
<div id="site_logo"> </div>
<h1 id="site_title">My CSOM App</h1>
</header>
<nav id="toolbar">
<asp:Button ID="cmdGetSiteProperties" runat="server" Text="Get Site
Properties" OnClick="cmdGetSiteProperties_Click" />
<asp:Button ID="cmdGetLists" runat="server" Text="Get Lists" OnClick="cmdGetLists_Click" />
<asp:Button ID="cmdCreateCustomersList" runat="server" Text="Create Customers
List" OnClick="cmdCreateCustomersList_Click" />
</nav>
<div id="content_box">
<asp:Literal ID="placeholderMainContent" runat="server"></asp:Literal>
</div>
</div>
</form>
</body>
</html>
8. open 'Pages" -> 'Default.aspx.cs' page and add below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;
namespace MyCSOMAppWeb
{
public partial class Default : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e) { }
protected void Page_Load(object sender, EventArgs e)
{
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
this.HostWebLink.NavigateUrl =
spContext.SPHostUrl.AbsoluteUri;
this.HostWebLink.Text = "Back
to Host Web";
}
protected void cmdGetSiteProperties_Click(object sender, EventArgs e)
{
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext =
spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web, web => web.Title, web =>
web.Url, web => web.Id);
clientContext.ExecuteQuery();
placeholderMainContent.Text = "<div>Host web title: " + clientContext.Web.Title + "</div>" +
"<div>Host web URL: " +
clientContext.Web.Url + "</div>" +
"<div>Host web ID: " +
clientContext.Web.Id + "</div>";
}
}
protected void cmdGetLists_Click(object sender, EventArgs e)
{
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext =
spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web);
ListCollection Lists = clientContext.Web.Lists;
clientContext.Load(Lists, lists
=> lists.Where(list => !list.Hidden).Include(list => list.Title,
list =>
list.DefaultViewUrl));
clientContext.ExecuteQuery();
string html = "<h2>Lists
in host web</h2>";
html += "<ul>";
foreach (var list in Lists)
{
html += "<li>" +
list.Title + "</li>";
}
html += "</ul>";
placeholderMainContent.Text =
html;
}
}
protected void cmdCreateCustomersList_Click(object sender, EventArgs e)
{
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (ClientContext clientContext =
spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();
string listTitle = "Customers";
ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext);
using (scope.StartScope())
{
using (scope.StartTry())
{
clientContext.Web.Lists.GetByTitle(listTitle).DeleteObject();
}
using (scope.StartCatch()) { }
}
ListCreationInformation listInformation = new ListCreationInformation();
listInformation.Title =
listTitle;
listInformation.Url = "Lists/Customers";
listInformation.QuickLaunchOption = QuickLaunchOptions.On;
listInformation.TemplateType =
(int)ListTemplateType.Contacts;
List list = clientContext.Web.Lists.Add(listInformation);
list.OnQuickLaunch = true;
list.EnableAttachments = false;
list.Update();
clientContext.ExecuteQuery();
ListItemCreationInformation lici1 = new ListItemCreationInformation();
var item1 = list.AddItem(lici1);
item1["Title"] = "Lennon";
item1["FirstName"] = "John";
item1.Update();
ListItemCreationInformation lici2 = new ListItemCreationInformation();
var item2 = list.AddItem(lici2);
item2["Title"] = "McCartney";
item2["FirstName"] = "Paul";
item2.Update();
clientContext.ExecuteQuery();
placeholderMainContent.Text = "<div>New list created: </div><a
href='" +
spContext.SPHostUrl.AbsoluteUri + listInformation.Url + "'>Click Here</a>";
}
}
}
}
10. perss 'F5' to deply and test.
No comments:
Post a Comment