Sunday, June 15, 2014

Server side Developerment using CAML and Linq

Server side API is only supported in Farm solution and sandbox solution.'

Client side(Sharepoint Apps) code development requires  ECMAScript library such as jQuery or Microsoft AJAX .

Debugging Tools : IE developer Tools, firefox  fire bug adon.

Server side Development :
CAML (Collaborative Application Markup Language (CAML) is an XML-based language)
LINQ (Language-Integrated Query)

Most common object in  CAML  : SpQuery,SpSiteDataQuery

Search API is usefull when you query against cross site or large list.

Example of Spquery :

 
 using (SPWeb web = site.OpenWeb())
            {
               // Build a query.
               SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />",
                              "</OrderBy>");                   

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.
               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks";
               SPList list = web.GetList(listUrl);
               SPListItemCollection items = list.GetItems(query);

               // Print a report header.
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                  "Assigned To", "Task", "Due Date", "Priority");

               // Print the details.
               foreach (SPListItem item in items)
               {
                  Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                     item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Priority"]);
               }
            }
         }
 using (SPWeb web = site.OpenWeb())
            {
               // Build a query.
               SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />",
                              "</OrderBy>");                   

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.
               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks";
               SPList list = web.GetList(listUrl);
               SPListItemCollection items = list.GetItems(query);

               // Print a report header.
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                  "Assigned To", "Task", "Due Date", "Priority");

               // Print the details.
               foreach (SPListItem item in items)
               {
                  Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                     item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Priority"]);
               }
            }
         }



SpsiteDataQuery:

This is used to get the data frorm multiple list across multiple sites.


SPSiteDataQuery query = new SPSiteDataQuery();
            query.Webs = "<Webs scope='SiteCollection' />";
            query.ViewFields = @"<FieldRef Name='Title' /> <FieldRef Name='FirstName' />";
            query.Query = @"<Where> <FieldRef Name='ContentType'/> <Value Type='Computed'>Contact</Value> </Where>";
            DataTable results = SPContext.Current.Web.GetSiteData(query);
            foreach (DataRow item in results.Rows)
            {
               Console.Write(item["FirstName"].ToString());
            }



Linq query will return IEnumerable<T> interface. Example :


All collection will implement IEnumerable<T>

// string array


String[] Col1 = new string[] { "a", "b", "c" };
// list collection
List<string> Col2 = new List<string>()
{ "a", "b", "c"};
// queue collection
Queue<string> Col3 = new Queue<string>();

Col3.Enqueue("a");Col3.Enqueue("bCol3.Enqueue("c);

Displaycol1(Col1); // This method will  IEnumerable type.
Displaycol1(Col2);
Displaycol1(Col3);




Linq Extension method :

Extension methods feature added to .NET 3.0
Allows developers to extend existing types
LINQ adds extension methods to IEnumerable<T>

Out put of Extension method :










Linq default it provide nearly 50 extension methods .



More Enumerable Methods method are found in : http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods%28v=vs.110%29.aspx




Where Condition in LinQ can be used in following technique.


Anonymous Delegate
Lambda expression
Explicit delegate
 



            IEnumerable<string> cal1, cal2, cal3, cal4;
            cal1 = new List<string>() { "a", "b", "c", "d" };

   // technique : named delegate & WHERE method

            Func<string, bool> delegateFilterMethod = StringHasFourCharacters;
            cal2 = cal1.Where(delegateFilterMethod);
 
    // technique : anonymous delegate
    
            cal3 = cal1.Where(delegate(string value)
            {
                return (value.Length == 4); }
            );
   // technique : lambda
            cal4 = cal1.Where(value => value.text =="txt" );
        }

Linq support  custom object :

class Customer
{
    // Auto-Impl Properties for trivial get and set 
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }
}


OLD way of creaating initial objects 


 Customer c2 =new  customer();
c2.TotalPurchases ="34.45";
c2.Name="text";
c2.CustomerID=20;


New way of creaating initial objects 
Customer c2 =new customer
{
c2.TotalPurchases ="34.45";
c2.Name="text";
c2.CustomerID=20;
};




LinQ Sample Example  : 

 Click Here  : http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b







Linq to Sharepoint  :


SPMetal.exe generate strongly-typed entity classes

Each entity class represents one SharePoint list

SPMetal.exe also generates DataContext class


Some Example :



// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);

// Get the SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");

// Query for customers from London
var londonCustomers = from customer in Customers
                      where customer.City == "London"
                      select customer;

foreach (var londonCust in londonCustomers)
{
    Console.Writeline("id = {0}, City = {1}",
                      londonCust.CustomerId,
                      londonCust.City);
}



The following is an example of using LINQ to add an item to a SharePoint Foundation list




// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);

// Get the SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");

// Create the item to be added
Customer newCustomer = new Customer() { CustomerId=36, City=”Madrid” };

// Mark the item to be added on the next call of Submit
Customers.InsertOnSubmit(newCustomer);

// Submit all changes
data.SubmitChanges();

More information About Linq to sharepoint : http://msdn.microsoft.com/en-us/library/office/ee535491(v=office.14).aspx



No comments:

Post a Comment