Elczar 的个人资料Sharing Points { }照片日志列表 工具 帮助

日志


9月5日

Programming SharePoint List Data

 

Windows SharePoint Services 3.0 is bundled with lists and libraries. A list is a collection of information, including announcement, discussion board, and calendar. A library, on the other hand, is a location on a SharePoint site where we can create, collect, update, and manage files, including document library, picture library, and form library. On top of lists and libraries is the Windows SharePoint Services object model that consists of more than few assemblies and namespaces - that can used to programmatically interact with our SharePoint site components, including SharePoint lists and libraries.

Below is the list of assemblies:

 

Windows SharePoint Services 3.0

Microsoft Office SharePoint Server 2007

Microsoft.SharePoint.dll Microsoft.Office.Server.dll
  Microsoft.Office.Server.Publishing.dll
  Microsoft.Office.Server.Policy.dll
  Microsoft.Office.Server.Search.dll
  Microsoft.Office.Workflow.Tasks.dll
  Microsoft.SharePoint.Portal.dll
  Microsoft.SharePoint.Publishing.dll
  Microsoft.SharePoint.Workflow.Actions.dll

Another component that we need to understand before writing our first line of code is the server-side object model structure of SharePoint site allied to lists and libraries. The following table shows the SharePoint site object hierarchy in relation to the collection and objects of the Microsoft.SharePoint namespace.

 

Site Collection Each SPSite object, despite its singular name, represents a set of logically related SPWeb objects commonly called a "site collection".
Top-Level Web Site / Subsites Each site collection includes any number of SPWeb objects, and each object has members that can be used to manage a site. The Webs property returns an SPWebCollection object that represents all the subsites of a specified site, and the Lists property returns an SPListCollection object that represents all the lists in the site.
Lists Each SPList object has members that are used to manage the list or access items in the list. The GetItems method can be used to perform queries that return specific items. The Fields property returns an SPFieldCollection object that represents all the fields, or columns, in the list, and the Items property returns an SPListItemCollection object that represents all the items, or rows, in the list.
Fields / List Items Each SPField object has members that contain settings for the field. While each SPListItem object represents a single row in the list.

Having discussed some introductory concepts, we could now easily comprehend the following codes that programmatically insert and retrieve SharePoint list data.

Data Insertion

using (SPSite spsite = new SPSite(http://SPS01))

{

using (SPWeb spweb = spsite.OpenWeb())

{

spweb.AllowUnsafeUpdates = true;

SPList splist = spweb.Lists["Custom List"];

SPListItem splistitem = splist.Items.Add();

splistitem["Title"] = "Paul Joseph Adame";

splistitem["First_x0020_Name"] = "Luke Martin";

splistitem["Last_x0020_Name"] = "Adame";

splistitem.Update();

}

}

Data Retrieval

using (SPSite spsite = new SPSite("http://SPS01"))

{

using (SPWeb spweb = spsite.OpenWeb())

{

spweb.AllowUnsafeUpdates = true;

SPSiteDataQuery spsitedataquery = new SPSiteDataQuery();

spsitedataquery.Lists = "<Lists BaseType='0'/>";

spsitedataquery.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";

spsitedataquery.Webs = "<Webs Scope='SiteCollection' />";

spsitedataquery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='First_x0020_Name' /><FieldRef Name='Last_x0020_Name' />";

spsitedataquery.RowLimit = 10;

DataTable datatable = spweb.GetSiteData(spsitedataquery);

gvwList.DataSource = datatable;

gvwList.DataBind();

}

}

Notice that a Collaborative Application Markup Language (CALM) is being passed to the query property of the SPSiteDataQuery. CALM is an XML-based language that is used in Windows SharePoint Services to define fields and views. In our case, we use CALM to define queries against list data. Below is the query schema:

 

PARENT ELEMENT

ELEMENT

DESCRIPTION

Where   Used within the context of a query to specify a filter.
  And Used within the Where element to group filters in a query for a view.
  Or Used within the Where element to group filters in a query.
  BeginsWith Searches for a string at the start of a column that holds Text or Note field type values.
  Contains Searches for a string anywhere within a column that holds Text or Note field type values.
  Eq Arithmetic operator that means "equal to" and is used within a query.
  Geq Arithmetic operator that means "greater than or equal to." This element can be used within a Where element in a query.
  Gt Arithmetic operator that means "greater than." This element is used similarly to the Eq and Lt elements.
  Leq Arithmetic operator that means "less than or equal to." The Leq element is used in view queries similarly to the Eq and Geq elements.
  Lt Arithmetic operator that means "less than" and is used in queries in views. This element is used similarly to the Eq and Gt elements.
  Neq Arithmetic operator that means "not equal to" and is used in queries.
  DateRangesOvelap Used in queries to compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap.
  IsNotNull Used within a query to return items that are not empty (Null).
  IsNull Used within a query to return items that are empty (Null).
OrderBy   Determines the sort order for a query. The OrderBy element contains a group of FieldRef elements.
GroupBy   Contains a Group By section for grouping the data returned through a query in a list view.

Thanks.

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
AdameElcza​r 在此页禁用了评论功能。

引用通告

此日志的引用通告 URL 是:
http://elczara.spaces.live.com/blog/cns!554EC06D366AC9D5!681.trak
引用此项的网络日志