Monday, 8 October 2012

Client Side Code Samples

Equivalent Objects in Server and Client OMs







SERVER OM
.NET MANAGED
ECMASCRIPT
Microsoft.SharePoint.SPContext
Microsoft.SharePoint.Client.ClientContext
SP.ClientContext
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.Client.Site
SP.Site
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.Client.Web
SP.Web
Microsoft.SharePoint.SPList
Microsoft.SharePoint.Client.List
SP.List
Microsoft.SharePoint.SPListItem
Microsoft.SharePoint.Client.ListItem
SP.ListItem
Microsoft.SharePoint.SPField
Microsoft.SharePoint.Client.Field
SP.Field
Which DLLs Implement the Client OM
Client OM located under  %Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI .
There are two DLLs for the managed OM, Microsoft.SharePoint.Client and Microsoft .SharePoint.Client.Runtime .
Add references to VS.
Ex:1
Retrieving site titles and its Urls
ClientContext context = new Microsoft.SharePoint.Client.ClientContext("http://Mohan:8089");
Web site = context.Web;
context.Load(site);
context.ExecuteQuery();
MessageBox.Show("Title: " + site.Title + " Relative URL: " + site.ServerRelativeUrl);
context.Dispose();
List Collections
ListCollection lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();
MessageBox.Show(lists.Count.ToString());
foreach (Microsoft.SharePoint.Client.List list in lists)
{
MessageBox.Show("List: " + list.Title);
}
Ex:2 List the Properties
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
Web site = context.Web;
context.Load(site, s = > s.Title, s = > s.ServerRelativeUrl);
ListCollection lists = site.Lists;
context.Load(lists, ls = > ls.Include(l = > l.Title));
context.ExecuteQuery();
MessageBox.Show("Title: " + site.Title + " Relative URL: " + site.ServerRelativeUrl);
MessageBox.Show(lists.Count.ToString());
foreach (Microsoft.SharePoint.Client.List list in lists)
{
MessageBox.Show("List: " + list.Title);
}
context.Dispose();
Ex:3
Using CAML Query
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
List list = context.Web.Lists.GetByTitle("Announcements");
ListItemCollectionPosition itemPosition = null;
while (true)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml = @"
< View >
< Query >
< Where >
< IsNotNull >
< FieldRef Name='Title' / >
< /IsNotNull >
< /Where >
< /Query >
< RowLimit > 1000 < /RowLimit >
< /View > ";
ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems);
context.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
foreach (ListItem listItem in listItems.ToList())
{
MessageBox.Show("Title: " + listItem["Title"]);
}
if (itemPosition == null)
{
break;
}
MessageBox.Show("Position: " + itemPosition.PagingInfo);
}
Ex:4
Using the LINQ
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
var query = from list
in context.Web.Lists
where list.Title != null
select list;
var result = context.LoadQuery(query);
context.ExecuteQuery();
foreach (List list in result)
{
MessageBox.Show("Title: " + list.Title);
}
context.Dispose();
Ex:-5
Creating Lists, Fields, and Items
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
Web site = context.Web;
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "New List";
listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
List list = site.Lists.Add(listCreationInfo);
Field newField = list.Fields.AddFieldAsXml(@" < Field Type='Text' DisplayName='NewTextField' > < /Field > ", true, AddFieldOptions.AddToDefaultContentType);
ListItemCreationInformation itemCreationinfo = new ListItemCreationInformation();
ListItem item = list.AddItem(itemCreationinfo);
item["Title"] = "My New Item";
item["NewTextField"] = "My Text";
item.Update();
context.ExecuteQuery();
context.Dispose();
Ex:-6
Deleting Lists and Items
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
List list = context.Web.Lists.GetByTitle("New List");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"
< View >
< Query >
< Where >
< IsNotNull >
< FieldRef Name='Title' / >
< /IsNotNull >
< /Where >
< /Query >
< /View > ";
ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems, items = > items.Include(item = > item["Title"]));
context.ExecuteQuery();
foreach (ListItem listItem in listItems.ToList())
{
listItem.DeleteObject();
}
context.ExecuteQuery();
context.Dispose();
Advantages
Less Deployment Hassles: Using Client OM, you do not need to install the components required by the Server Object Model. Thus Client OM provides much ease to the end user.
Language Flexibility: We can use the following languages to work with the Client OM:
Microsoft .NET
Silverlight
ECMA Script (JavaScript /JScript)
Query Speed Optimizations: In the Client OM, reduced network traffic is attained using Query Optimizations. Thus the user will feel reduced round trips and other advantages like paged results, etc.


Thursday, 6 September 2012

Feature Using the Custom Action Pages

In the above example (http://www.blogger.com/blogger.g?blogID=403610387301840191#editor/target=post;postID=5161020807956575523) I used the public site URL Action elements. In this example use own ASPX pages as target.
For this feature, create a Feature.xml file and Manifest file that adds a new link to the Site Settings Galleries section.
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
            Id="4C652168-BBCE-4B2A-BA02-B66814B272B8"
            Scope="Site"
            Title="Content Type Hierarchy Pages sample"
            Description="Show the Hierarch content type page sample"
            >
     <ElementManifests>
           <ElementManifest Location="Elements.xml"/>
     </ElementManifests>
</Feature>

Create the Elements.Xml file
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <CustomAction Id="9AE49699-675D-4DB8-8082-81C832E52D9B"
                       GroupId="Galleries"
                       Location="Microsoft.SharePoint.SiteSettings"
                       Sequence="0"
                       Title="Site Content type Hierarchy Pages."
                       Description="Display Site content Hierarchy of site content pages.">
           <UrlAction Url="_layouts/TestPage.aspx"/>
     </CustomAction>
</Elements>


Create a TestPage.aspx and place into this location “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS”
Example TestPage.aspx code as follows
<%@ Page Language="C#"  %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% Response.Write("This is my custom page testing .."); %>
    </div>
    </form>
</body>
</html>
Install the feature, and activate the feature à Once you activated the feature, You will find a New link in the Galleries of the top level site settings.
 

Create a Custom Feature

Creating a Custom Feature, Adding a Link in Site Actions

1. Go to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\Features Location

2. Add a new folder (ex: - MSDownLoads), with in this folder add two xml files with following code.

1. Feature.xml

<?xml version="1.0" encoding="utf-8" ?>
<
Feature xmlns="http://schemas.microsoft.com/sharepoint/"Id="F023AE13-83FE-4E4C-B011-8F0B47026431"Title="My Custom Feature"Description="Mor batch custome feature to download a ms-softwares"Hidden="FALSE"Scope="Web" Version="1.0.0.0"><
ElementManifests><
ElementManifest Location="Elements.xml"/></
ElementManifests>
</
Feature>
2. Elements.xml
<?xml version="1.0" encoding="utf-8" ?><
Elements xmlns="http://schemas.microsoft.com/sharepoint/"><
CustomAction Id="22BC8409-9429-49D2-A629-CB6AB2159A9B"GroupId="SiteActions"Location="Microsoft.SharePoint.StandardMenu"Title="Downloads" Description="MSdownloads"Sequence="12000"><
UrlAction Url="Http://www.microsoft.com"/>
</
CustomAction> </
Elements>
3. Now go to the command prompt change to this location (C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\bin) and install the feature using with STSADM.EXE

         Ex: STSADM.EXE -o installfeature -foldername(MSDownLoads)
         You will get operation completed successfully

4. Now go to the web application (http://Mohan:8087), click on any sub site, click on Site Actions, Site Settings.

Under Site Actions Section, Click on Manage Site Features, Find
My Custom Feature feature Click on Activate.

Now Click on Site Actions, You will find a Link Downloads, After Site Setting.