Skip to main content

Sharepoint Programming Part7

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

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...