Example 11:
The following example queries all lists that were created with
the Contacts list template anywhere in the site collection,
retrieves the first and last names of every contact,
and displays that information.
using System;
using Microsoft.SharePoint;
using System.Data;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPSiteDataQuery query = new SPSiteDataQuery();
//Ask for all lists created from the contacts template.
query.Lists = "<Lists ServerTemplate=\"105\" />";
// Get the Title (Last Name) and FirstName fields.
query.ViewFields = "<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
// Note that setting the Nullable attribute to TRUE
// causes an empty value to be returned for lists that
// do not include the FirstName column. The default is
// to skip a list that does not include the column.
// Set the sort order.
query.Query = "<OrderBy>" +
"<FieldRef Name=\"Title\" />" +
"</OrderBy>";
// Query all Web sites in this site collection.
query.Webs = "<Webs Scope=\"SiteCollection\" />";
DataTable dtContacts =
web.GetSiteData(query);
foreach (DataRow dr in dtContacts.Rows)
{
Console.WriteLine("Last Name : {0}",
dr["Title"]);
Console.WriteLine("First Name : {0}",
dr["FirstName"]);
}
}
}
}
}
}
Example 12:
The following example performs Insert, Update, and Delete operations
on a Products List using Linq to SharePoint.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Linq;
using Peers.SP2013.LinqToSP;
namespace DMLLinqinSP
{
class Program
{
static void Insert()
{
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Get the list from the site
EntityList<ProductsItem> listItems =
PeersDataContext.GetList<ProductsItem>("Products");
//Create a new item
ProductsItem newItem = new ProductsItem()
{
Title = "Hardware",
ProductID = "5",
ProductName = "RAM"
};
// Insert the new list item to the list
listItems.InsertOnSubmit(newItem);
//Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Inserted");
}
static void Update()
{
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Querying the list item that has to be updated
var updateItem = (from item in PeersDataContext.Products
where
item.ProductID == "1"
select item).First();
updateItem.ProductID = "6";
updateItem.ProductName = "MotherBoard";
// Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Updated");
}
static void Delete()
{
// Create an instance
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Get the list from the site
EntityList<ProductsItem> listItems =
PeersDataContext.GetList<ProductsItem>("Products");
// Querying the list item that has to be deleted
var updateItem = (from item in PeersDataContext.Products
where
item.ProductID == "3"
select item).First();
// Deleting the list item
listItems.DeleteOnSubmit(updateItem);
// Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Deleted");
}
static void Main(string[] args)
{
Insert();
Update();
Delete();
}
}
}
The following example queries all lists that were created with
the Contacts list template anywhere in the site collection,
retrieves the first and last names of every contact,
and displays that information.
using System;
using Microsoft.SharePoint;
using System.Data;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPSiteDataQuery query = new SPSiteDataQuery();
//Ask for all lists created from the contacts template.
query.Lists = "<Lists ServerTemplate=\"105\" />";
// Get the Title (Last Name) and FirstName fields.
query.ViewFields = "<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
// Note that setting the Nullable attribute to TRUE
// causes an empty value to be returned for lists that
// do not include the FirstName column. The default is
// to skip a list that does not include the column.
// Set the sort order.
query.Query = "<OrderBy>" +
"<FieldRef Name=\"Title\" />" +
"</OrderBy>";
// Query all Web sites in this site collection.
query.Webs = "<Webs Scope=\"SiteCollection\" />";
DataTable dtContacts =
web.GetSiteData(query);
foreach (DataRow dr in dtContacts.Rows)
{
Console.WriteLine("Last Name : {0}",
dr["Title"]);
Console.WriteLine("First Name : {0}",
dr["FirstName"]);
}
}
}
}
}
}
Example 12:
The following example performs Insert, Update, and Delete operations
on a Products List using Linq to SharePoint.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Linq;
using Peers.SP2013.LinqToSP;
namespace DMLLinqinSP
{
class Program
{
static void Insert()
{
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Get the list from the site
EntityList<ProductsItem> listItems =
PeersDataContext.GetList<ProductsItem>("Products");
//Create a new item
ProductsItem newItem = new ProductsItem()
{
Title = "Hardware",
ProductID = "5",
ProductName = "RAM"
};
// Insert the new list item to the list
listItems.InsertOnSubmit(newItem);
//Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Inserted");
}
static void Update()
{
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Querying the list item that has to be updated
var updateItem = (from item in PeersDataContext.Products
where
item.ProductID == "1"
select item).First();
updateItem.ProductID = "6";
updateItem.ProductName = "MotherBoard";
// Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Updated");
}
static void Delete()
{
// Create an instance
PeersDataContext PeersDataContext = new
PeersDataContext("http://sp2013dev/sites/peers487");
// Get the list from the site
EntityList<ProductsItem> listItems =
PeersDataContext.GetList<ProductsItem>("Products");
// Querying the list item that has to be deleted
var updateItem = (from item in PeersDataContext.Products
where
item.ProductID == "3"
select item).First();
// Deleting the list item
listItems.DeleteOnSubmit(updateItem);
// Submit the changes
PeersDataContext.SubmitChanges();
Console.WriteLine("Item Deleted");
}
static void Main(string[] args)
{
Insert();
Update();
Delete();
}
}
}
No comments:
Post a Comment