Elczar Adame's Shared Points on SharePoint

Archive for September, 2008

Programming SharePoint Library File


 

In my previous blog, I have mentioned that Windows SharePoint Services 3.0 / Microsoft Office SharePoint Server 2007 is bundled with lists and libraries. A SharePoint library, as I have mentioned, 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 this post, I have tabulated the assemblies and components necessary for SharePoint library files programming.

In this piece, I will attempt to provide sample codes on the following:

1. Creating a subfolder in a SharePoint library (parent folder).

using (SPSite spsite = new SPSite("http://SPS01"))
        {
            using (SPWeb spweb = spsite.OpenWeb())
            {
                spweb.AllowUnsafeUpdates = true;

                SPList splist = spweb.Lists["Shared Documents"];
                SPListItem splistitem = splist.Items.Add(splist.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "Demonstration Folder");             

                splistitem.Update();
            }
        }

2. Uploading file in a SharePoint library.

using (SPSite spsite = new SPSite("http://SPS01"))
        {
            using (SPWeb spweb = spsite.OpenWeb())
            {
                spweb.AllowUnsafeUpdates = true;

                SPFolder spfolder = spweb.Folders[Site + "/Shared Documents/"];
                byte[] content = null;
                using (FileStream filestream = new FileStream("C:/Sample.docx", System.IO.FileMode.Open))
                {
                    content = new byte[(int)filestream.Length];
                    filestream.Read(content, 0, (int)filestream.Length);
                    filestream.Close();
                }

                SPFile spfile = spfolder.Files.Add("Sample.docx", content, true);

                //Upload file in subfolder.
                //SPFile spfile = spfolder.SubFolders["Demonstration Folder"].Files.Add("Sample.docx", content, true);                  
            }
        }

3.  Deleting file from a SharePoint library.

using (SPSite spsite = new SPSite("http://SPS01"))
        {
            using (SPWeb spweb = spsite.OpenWeb())
            {
                spweb.AllowUnsafeUpdates = true;

                SPFolder spfolder = spweb.Folders[Site + "/Shared Documents/"];
                //spfolder.Files["Sample.docx"].Delete();

                //Recycle file from parent folder.
                //spfolder.Files["Sample.docx"].Recycle();

                //Delete file from subfolder.
                //spfolder.SubFolders["Demonstration Folder"].Files["Sample1.docx"].Delete();

                spfolder.Update();
            }
        }

Thanks…

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.

Best Practices Resource Center for SharePoint Server 2007


 

Thanks to Microsoft Consulting Services and SharePoint Customer Team, part of the core product team, for providing us best practices guidance to avoid common pitfalls and keep our Microsoft Office SharePoint Server 2007 environment available and performing well:

1. Operational Excellence

2. Team Collaboration Sites

3. Publishing Portals

4. Search

5. My Sites

6. Developing Custom Applications

7. Additional Resources

Hope this would server as a reference.

Tag Cloud