Sharepoint Programming

Q) Retrieving information from linked lists in SharePoint using LINQ to SharePoint?

using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;


In the Page_Load method, add the following code:


DataContext dc = new DataContext(SPContext.Current.Web.Url);
var customers = dc.GetList < Customer > (“Customers”);
var customerDataSource = from customer in customers
select new {CustomerName = customer.Title,
ContactLastName = customer.PrimaryContact.Title,
ContactFirstName = customer.PrimaryContact.
FirstName};
CustomerGridView.DataSource = customerDataSource;
CustomerGridView.DataBind()


Q)How you can retrieve the name of each folder in the top - level site of a site collection, as well as the number of subfolders that each folder contains?



using System;
using System.Text;
using Microsoft.SharePoint;
namespace Wrox.SixInOne
{
class Program
{
static void Main(string[] args)
{
StringBuilder folderName = new StringBuilder();
using (SPSite site = new SPSite(“http://intranet/”))
{
using (SPWeb web = site.RootWeb)
{
foreach (SPFolder folder in web.Folders)
{
folderName.Append(folder.Name);
folderName.Append(“ “);
folderName.Append(folder.SubFolders.Count);
folderName.Append(“ < br > ”);
}
}
}
}
}
}

Creating a New SPSite Object (SitesAndWebs.cs)
using (SPSite intranetSiteCollection = new SPSite(“http://intranet/”))
{
SPWebCollection websCollection = siteCollection.AllWebs;
foreach (SPWeb web in websCollection)
{
Console.WriteLine(web.Title);
web.Dispose();
}
Console.Read();
}


//Deletes a site collection used for testing
using (SPSite siteCollection = new Site(“http://intranet/sites/test”))
{
siteCollection.Delete();
}
//Deletes a site in the current site collection
using (SPWeb testSite = SPContext.Current.Site.OpenWeb(“test”))
{
testSite.Delete();
}
//Deletes a list called “Invoices” in the current site
SPContext.Current.Web.Lists[“Invoices”].Delete();


//Deletes a list item with a title of “My List Item”
SPList myList = SPContext.Current.




Q)You could use the SharePoint server object model to create a new Yes/No column,
like this:
using (SPSite siteCollection = new SPSite(“http://intranet/”))
{
SPWeb rootWeb = siteCollection.RootWeb;
rootWeb.Fields.Add(“New Field”, SPFieldType.Boolean, true);
}


Q)Sharepoint provides two query objects that use CAML queries. These are SPQuery and
SPSiteDataQuery . SPQuery can query only a single list whereas SPSiteDataQuery can be used to
query data throughout a site collection. Both objects have a Query property that accepts CAML
query text as a string value.
You can create an SPQuery object and pass it in as a parameter to the SPList.GetItems() method
to retrieve all the items (sorted and grouped as you ’ ve specifi ed) that meet the appropriate criteria
in the list. The following example shows a query that returns all the list items from a list where the
Title column value starts with the word “ Policy. ”
string camlQueryText =
“ < Where > < BeginsWith > < FieldRef Name=’Title’/ > ” +
“ < Value Type=’Text’ > Policy < /Value > < /BeginsWith > < /Where > ”;
SPQuery camlQuery = new SPQuery();
camlQuery.Query = camlQueryText;
SPListItemCollection items = list.GetItems(camlQuery);
The SPSiteDataQuery object works in a similar way, except you can use it to retrieve list items
from multiple lists. You can choose either to retrieve items from just one site, or to retrieve
items from that site and any child sites.
Both query objects also provide you with the ability to specify which fi elds you would like returned
as well as a limit to the number of items you want returned.




One of the most compelling reasons for using LINQ to SharePoint instead of CAML is that LINQ
provides strongly typed classes, whereas CAML is written as a string of text that will be executed onlyat runtime. Visual Studio ’ s IntelliSense will help you program against your lists and their columns in
an easy manner, and will alert you at compile time if you are referencing a list or column incorrectly.
In addition to being easier to write, LINQ provides a more readable syntax than CAML.

Comments

Popular posts from this blog

SharePoint: Group By on more than 2 columns in a view (SP 2010)

Configure Form Based Authentication (FBA) in SharePoint 2010

SharePoint 2010 Windows PowerShell Interview Questions