LinkedIn

Tuesday, December 10, 2013

SharePoint : Representational State Transfer (REST)

The SharePoint 2010 Representational State Transfer (REST) interface is a WCF Data Service that allows you to use construct HTTP requests to query SharePoint list data. Representational State Transfer, aka REST, is an architectural style for web-based data access, an alternative to other techniques like SOAP Web Services and Remote Procedure Calls. OData is a protocol – a standardized way to implement REST to surface, query, and manipulate data. OData is an open web-data protocol developed by Microsoft. OData is all about web-based data access; OData can make almost any kind of structured data collection available to any kind of platform, because all data access is via plain old HTTP, and the data is served up as XML (or JSON) in an Atom-style RSS feed.
ODataQuery by URL, Answer by RSS
OData is built on the Entity Data Model. A RDBMS like SQL Server, tables contain rows; in OData, Collections contain Entries. In a database, tables can be related; in OData, Collections can be associated. A row has columns. An Entry has properties. Tables may have keys; Collections always have keys. Browsing to the service root of an OData service usually displays an Atom+XML list of all available Collections.
Entity Data Model (EDM) and OData have parallel terms:
EDM (What to query)
OData (What is returned)
Entity Set
Collection
Entity Type
Entry
Property of an Entity Type
Property of an Entry
Navigation Property
Link
Function Import
Service Operation
An OData Url has three parts: a Service root, a resource path, and (optionally) query string options. We”ve seen the service root, which typically returns a list of all available Collections. The resource path is kind of like a relative URL, and identifies a Collection or a single Entry or a property of an Entry. Basically the resource path drills down through the entity model to get to a particular object. OData URLs are usually case-sensitive, so take care with spelling. For example, http://services.odata.org/Northwind/Northwind.svc/Customers(”ALFKI”) returns just the ALFKI Customer, which is a single Entry identified by its key

Reference:

http://www.mindsharp.com/blog/2012/07/sharepoints-rest-an-odata-overview/

Writing Event Handlers for a Specific Sharepoint 2010 List

When you register Event handlers get added to all the lists of that template. for eg: Lets take a common scenario. Lets say you will have at least 2 custom lists in your site List1, List2. Now you write an event handler that you want to trigger on operations on one list List1. But this event handler also gets attached to List2 without you intending for.
Solution:
            There are may solutions you may find for this. One and most straight forward is write a feature stapler. But again writing a stapler is not easy and takes a lot of time. Also it's one more feature added to your site.
            The second solution(not too obvious) also exists. Now if you are writing a feature you got to have a Elements.xml file(At least I have to have one). Here making some tweaks will solve your problem.
I have created a solution called GG.BlogsEventHandler. I have written Asynchronous receiver. Below is how my Elements.xml looks like.



Look carefully at line #4. Do you see any difference? By default you will have


where xx=template id of your list. Custom Lists have "301" . Instead of giving a template Id, I have given site relative Url of the List.

Developing for performance in our SharePoint 2010

Some key concepts when it comes to developing for performance in our SharePoint 2010 applications.
SharePoint 2010 Developer Dashboard
The developer dashboard is a perfect tool for anyone who wants a quick way to access information about what goes on while rendering a page in SharePoint. It contains information about Web Parts, events, DB calls and a whole lot of nifty information.
Activating the Developer Dashboard
Developer Dashboard is a utility that is available in all SharePoint 2010 versions, and can be enabled in a few different ways:
PowerShell
STSADM.exe
SharePoint Object Model (API’s)
SPMonitoredScope to track performance in your applications
The monitored scope (SPMonitoredScope) is a class you can use to monitor performance and resource usage in your applications by using it in a simple code block.
The pros about using the monitored scopes are of course that you can easily track down and find bottlenecks as well as do some initial performance monitoring while doing your development.
Using SPMonitoredScope
In order to use the SPMonitoredScope you’ll need to add a using statement for Microsoft.SharePoint.Utilities and then wrap your code in a statement like this:
using (new SPMonitoredScope("CallMethod1 Monitored Scope"))
{
    Controls.Add(new Literal {Text = "Awesomeness
"});
}
You don’t need to add any more code than this in order for it to be hooked up.
Some caching techniques in SharePoint 2010

In SharePoint 2010, there’s a few different ways to cache data. In this section you can read about a few of those approaches. Most of the caching techniques I use regularly as a developer derives from or is a direct usage of the capabilities of the .NET framework.
Output Caching (Configurable)
The concept of Output Caching is something that natively comes with SharePoint 2010, as it builds on and relies on ASP.NET caching techniques. This means that you can configure your SharePoint 2010 site to cache the Pages it outputs. The reasoning behind caching a page is obviously that it takes time to generate the content on any page, and on a heavily accessed site it would be a performance impact to generate a new page on every request – that’s where Output Caching comes in handy.
BLOB cache overview
The disk-based BLOB cache controls the caching for binary large objects (BLOBs), such as frequently used image, audio, and video files, and other files that are used to display web pages, such as .css and .js files. The BLOB cache is enabled on a front-end web server and improves performance by retrieving BLOB files from the database and storing them in a directory on the front-end web end server where they are served to users. This reduces the network traffic to and load on the database server.
For example, if you have an Internet-facing portal with read-only files such as .doc or .pdf files, you can specify that those files be cached so that they are displayed more quickly to users. If you have a collaboration site that contains files that are frequently updated, and also media assets, you can specify that the cache is to store only audio or video types by including only file name extensions for those files in the cache settings.

Enable the BLOB cache

The BLOB cache is configured in the web.config file for each Web application and, by default, is not enabled. You must specifically enable the BLOB cache in order to get the performance advantage it provides.

Specify the size of the BLOB cache

When you decide how large to make the BLOB cache, you must consider the number and size of the files to determine the total size of the data to be stored in the cache. By default, the BLOB cache is set to 10 gigabytes (GB). Allow at least 20 percent more space on the drive than the size of the cache. For example, if you have 10 GB of content, set the size of the cache to 12 GB on a drive that has at least 15 GB of space. If the BLOB cache is too small, serving files to users slows, reducing the performance of your site.

Bit Rate Throttling
This section contains information about Bit Rate Throttling, describes when you should use it with the SharePoint solution, and explains how to enable it.

Bit Rate Throttling overview

Bit Rate Throttling is an IIS 7.0 extension that meters the download speeds of media file types and data between a server and a client computer. The encoded bit rates of media file types such as Windows Media Video (WMV), MPEG-4 (MP4), and Adobe Flash Video, are automatically detected, and the rate at which those files are delivered to the client over HTTP are controlled according to the Bit Rate Throttling configuration.

Decide to use Bit Rate Throttling

If you will make long-playing video assets available to users in SharePoint Server 2010, enable Bit Rate Throttling in IIS. Without Bit Rate Throttling, IIS will serve video files by using as much bandwidth as it can, which will result in increased network performance

Maximum upload file size

This section describes the upload file size limitation, tells how to decide what the maximum upload file size limit should be, and how to configure it.

Maximum upload file size overview

The maximum upload file size is a setting that is used by the SharePoint Server 2010 Web application that specifies the maximum size of a file that a user can upload to the server. When a new Web application is created, SharePoint Server 2010 sets the default maximum upload size to 50 MB.

Caching in code (Programmable)

While we know that there’s pre-configurable caching available in SharePoint 2010 (like the Output Cache and BLOB Cache), there’s obviously still a need to create custom caching routines in your applications.
In order for your custom applications to run efficiently and save on server load, you need to consider the importance of using proper caching in your applications.
For instance, if you’ve created an application that is (on every request) fetching information from a database or a SharePoint list (SPList), do you really need that data to be fetched directly from the source – or could you live with having it cached for a few minutes? If it’s not super-important data we’re dealing with that doesn’t need to be up to date every second and every request – please consider building some caching mechanisms in your applications

Developer Dashboard is a utility that is available in all SharePoint 2010 versions, and can be enabled in a few different ways:
a)     PowerShell
b)     STSADM.exe
c)      SharePoint Object Model (API’s)
SharePoint 2010 and CSS Sprites

CSS sprites is a technique used to reduce the number of requests for images on a site when visiting it. Take a bunch of separate images and smack them together to one single file. Voila, you’ll only need to request one file from the server. CSS sprites are a natural part of a normal SharePoint 2010 installation. By default, there’s CSS sprite techniques used to render and display images in (for example) the Ribbon menu. CSS sprites are that you can develop web applications that can reduce the page load and enhance the performance of your sites and make them load quicker and be more responsive

SharePoint Timer Job – SPJobLockType

There are 3 SPJobLockType  available:

1.       SPJobLockType.None -- if you set it none, the instance will run in all the available servers in the Farm (e.g. Application Server Timer Job)
2.       SPJobLockType.ContentDatabase – this will cause 3 instances to be running in each of the Web-Frontends.
3.       SPJobLockType.Job – this will cause only one instance of the job to run on any of the front-end servers. (Note: it is possible to see multiple instances listed in the Job Status .. but if you look at the time it was last run.. only one would have run lately)

If you have to develop a job, you have to first decide on the type of lock you need for your job.

E.g. If your job does something with the files in the Web-Frontend server you might want to use a ContentDatabase lock.. or if you have something that manipulates the data in a list.. you will have to use Job lock.
Note: If you use other types of locks to manipulate the data in a list.. the multiple job instances will run simultaneously and cause Data Update conflict errors.
Note: If for any reason you re-deploy your job.. either put the dll directly in GAC or deploysolution.. make sure you restart the 'Windows Sharepoint Services Timer' service. (OWSTIMER.EXE)Note: The account used by the Timer service should have access to the Content Database.

Here are some sample code(s) of a custom timer job definition:
[Guid("{62FF3B87-654E-41B8-B997-A1EA6720B127}")]
class MyTimerJob1 : SPJobDefinition
{
    public MyTimerJob1()
        : base()
    { }

    public MyTimerJob1(string name, SPService service, SPServer server,
        SPJobLockType lockType) : base(name, service, server, lockType)
    { }

    public MyTimerJob1(string name, SPWebApplication webApplication, SPServer server,
        SPJobLockType lockType) : base(name, webApplication, server, lockType)
    { }

    public override void Execute(Guid targetInstanceId)
    {
        //Execute Timer Job Tasks
    }
}
Remember that the different server roles that we can find on a Sharepoint farm are:

  • Database Server: the server that hosts the Microsoft SQL Server database for the farm. Since Sharepoint Foundation is not typically installed in this server, no jobs will run here.
  • Web Front End Server: server where the Microsoft SharePoint Foundation Web Application service is running on.
  • Application Server: Any other Sharepoint server.
Here are a couple of examples on where the jobs will run depending on the parameters passed to the constructor:

//Job associated with a web app, no server in particular and none lock:
//  will run on all fron end servers.
var jobRunningOnAllFrontEndServers = new MyTimerJob1("mytimerjob", 
    SPWebApplication.Lookup(webAppURI), null, SPJobLockType.None);

//Job associated with a web app, one front end server and job lock:
//  will run only in the frontEndServer1 server.
var jobRunningOnAParticularFronEndServer = new MyTimerJob1("mytimerjob", 
    SPWebApplication.Lookup(webAppURI), fronEndServer1, SPJobLockType.Job);

//Job associated with a webApp, and an app server and lock type job: 
//  it won't run on any server since the server specified is NOT running 
//  the Web Application Service
var jobRunningOnNoServer = new MyTimerJob1("mytimerjob", 
    SPWebApplication.Lookup(webAppURI), appServer1, SPJobLockType.Job);

//Job associated with the timer service, a particular app server and none lock:
//  will run on the appServer1 server only.
var jobRunningOnAppServer = new MyTimerJob1("mytimerjob", 
    SPFarm.Local.TimerService, appServer1, SPJobLockType.None);

Tuesday, December 3, 2013

Business Connectivity Services security overview in SharePoint Server 2010

When a user accesses external data from a Web browser, three systems are involved: the logged on user’s client computer, the Web server farm, and the external system.

  1. From Web browsers, users typically interact with external data in external lists or by using Web Parts. 
  2. The BDC Server Runtime on front-end servers uses data from the Business Data Connectivity service to connect to and execute operations on external systems.
  3.  The Secure Store Service securely stores credential sets for external systems and associates those credential sets to individual or group identities.
  4. The Security Token Service is a Web service that responds to authentication requests by issuing security tokens made up of identity claims that are based on user account information.


The following table describes the authentication modes of the Microsoft Business Connectivity Services:

Authentication mode
Description
PassThrough
Passes the credentials of the logged-on user to the external system. This requires that the user’s credentials are known to the external system.
If the Web application is not configured to authenticate with Windows credentials, the NT Authority/Anonymous Logon account is passed to the external system rather than the user's credentials.
RevertToSelf
When the user is accessing external data from a Web browser, this mode ignores the user’s credentials and sends the application pool identity account under which the BCS runtime is running on the Web server to the external system. When the user is accessing external data from an Office client application, this mode is equivalent to PassThrough mode, because Microsoft Business Connectivity Services running on the client will be running under the user’s credentials.
This mode is called BDC Identity in the Microsoft Business Connectivity Services administration pages and in SharePoint Designer 2010.
WindowsCredentials
For external Web services or databases, this mode uses a Secure Store Service to map the user’s credentials to a set of Windows credentials on the external system.
Credentials
For an external Web service, this mode uses a Secure Store Service to map the user’s credentials to a set of credentials that are supplied by a source other than Windows and that are used to access external data. The Web service should use basic or digest authentication when this mode is used.
ImportantImportant:
To help preserve security in this mode, we recommend that the connection between the Microsoft Business Connectivity Services and the external system should be secured by using Secure Sockets Layer (SSL) or Internet Protocol Security (IPSec).
RDBCredentials
For an external database, this mode uses a Secure Store Service to map the user’s credentials to a set of credentials that are supplied by a source other than Windows. To help preserve security in this mode, we recommend that the connection between the Microsoft Business Connectivity Services and the external system should be secured by using Secure Sockets Layer (SSL) or IPSec.
DigestCredentials
For a WCF Web service, this mode uses a Secure Store Service to map the user’s credentials to a set of credentials using Digest authentication.
Performace Counters in Sharepoint

Objects and Counters
Problem
Resolution Options
Processor
Processor - % Processor Time
Over 75-85%
This shows processor usage over time. If this is consistently too high, you may find performance is adversely affected. Remember to count "Total" in multiprocessor systems. You can measure the utilization on each processor also, to ensure balanced performance between cores.
Upgrade processor
Increase number of processors
Add additional server(s)
Disk
Avg. Disk Queue Length
Gradually increasing, system not in a steady state and queue is backing up
This shows the average number of both read and write requests that were queued for the selected disk during the sample interval. A bigger disk queue length may not be a problem as long as disk reads/writes are not suffering and the system is working in a steady state without expanding queuing.
Increase number or speed of disks
Change array configuration to stripe
Move some data to an alternative server
% Idle Time
Less than 90%
Increase number of disks
Move data to an alternative disk or server
% Free Space
Less than 30%
Increase number of disks
Move data to an alternative disk or server
Memory
Available Mbytes
Less than 2GB on a Web server.
This shows how much physical memory is available for allocation. Insufficient memory leads to excessive use of the page file and an increase in the number of page faults per second.
Add memory.
noteNote:
SQL Server available memory will be low, by design, and does not always indicate a problem.
Cache Faults/sec
Greater than 1
This counter shows the rate at which faults occur when a page is sought in the file system cache and is not found.
Add memory
Increase cache speed or size if possible
Move data to an alternative disk or server
Pages/sec
Greater than 10
This counter shows the rate at which pages are read from or written to disk to resolve hard page faults. If this increases, it indicates system-wide performance problems.
Add memory
Paging File
% Used and % Used Peak
The server paging file, sometimes called the swap file, holds "virtual" memory addresses on disk. Page faults occur when a process has to stop and wait while required "virtual" resources are retrieved from disk into memory. These will be more frequent if the physical memory is inadequate.
Add memory
NIC
Total Bytes/sec
Over 40-50% of network capacity. This is the rate at which data is sent and received via the network interface card.
Investigate further by monitoring Bytes received/sec and Bytes Sent/sec.
Reassess network interface card speed
Check number, size, and usage of memory buffers
Process
Working Set
Greater than 80% of total memory.
This counter indicates the current size (in bytes) of the working set for a given process. This memory is reserved for the process, even if it is not being used.
Add memory
% Processor Time
Over 75-85%.
Increase number of processors
Redistribute workload to additional servers
ASP.NET
Application Pool Recycles
Several per day, causing intermittent slowness.
Make sure that you have not implemented settings that automatically recycle the application pool unnecessarily throughout the day.
Requests Queued
Hundreds or thousands of requests queued.
Implement additional Web servers
The default maximum for this counter is 5,000, and you can change this setting in the Machine.config file
Request Wait Time
As the number of wait events increases, users will experience degraded page rendering performance.
Implement additional Web servers
Requests Rejected
Greater than 0
Implement additional Web servers

References:
http://technet.microsoft.com/en-us/library/ff758658.aspx