Click here to Skip to main content
15,895,667 members
Articles / Web Development / ASP.NET
Article

SharePoint Object Browser

Rate me:
Please Sign up or sign in to vote.
3.18/5 (7 votes)
17 Apr 2008CPOL3 min read 73.9K   636   36   16
This application is a SharePoint site object browser which shows all the contents of a SharePoint site in a tree structure.

Introduction

This application is a SharePoint site object browser which shows all the contents of a SharePoint site in a tree structure. The main objective of this sample program is to show how to use a SharePoint object model. This sample code is a very easy view of SharePoint development. But to develop in SharePoint these are things a developer should know.

Background

There are lot's of samples and help for SharePoint administration and customization. But there are very few samples and examples in SharePoint programming. At the beginning, we really had to struggle to develop WebPart and to understand how object models and other SharePoint stuff works. I have been working on a SharePoint project for nearly one year and I felt like sharing some of my experiences with others using different sample applications. And this is my first example.

What this Application Does

This is a SharePoint site object browser application. In the textbox, followed by the label Site Address, enter a SharePoint site address and then press the Go button. You will see all subsites and content of subsites will be generated in a tree form in the TreeView control. In that control, if you click on a tree node of type list then the list's contents will be shown in the bottom DataGridView. In the Grid you will find all columns and rows of that particular list. Even some columns that you will see here, will not be visible from SharePoint site. You can also select a column from the DropDownList and can update the value for all rows for that particular column. But as I said, you will be able to see some SharePoint internal columns, and those values we cannot update.

Image 1

Understanding the Code

We can take a SharePoint site instance (SPSite) from the site's URL. Then we can access that SPSite object's properties.

C#
uxBrowser.Nodes.Clear();
SPSite site = new SPSite(uxAddress.Text);
TreeNode rootNode = uxBrowser.Nodes.Add(site.Url);
uxBrowser.NodeMouseClick += new TreeNodeMouseClickEventHandler(uxBrowser_NodeMouseClick);

for (Int32 i = 0; i < site.AllWebs.Count; i++)
{
   SPWeb web = site.AllWebs[i];
   TreeNode webNode = rootNode.Nodes.Add(web.Name);
   for (Int32 j = 0; j < web.Lists.Count; j++)
   {
      SPList list = web.Lists[j];
      TreeNode listNode = webNode.Nodes.Add(list.Title);
      listNode.Tag = list;
   }
}

As you can see, fist we are creating an instance of SPSite from a string (site URL) then we are getting AllWebs which is a collection of SPWeb objects. After that we loop through all SPWeb objects and its contents and extracting its information. While doing so, we are also adding these objects's names and references (saved in the Tag property) in the TreeView control to generate the tree.

Now if you click on the list type tree node, it will take the list reference from the Tag property.

C#
_CurrentSelectedList = ((SPList)e.Node.Tag);

Then the Fields property of _CurrentSelectedList will return the column collection of that list, and finally, _CurrentSelectedList.Items will return all rows (items) in that list. To make the data more presentable, I have added all contents in a DataTable then bound it to the DataGridView.

When the user clicks on the list type tree node, I am also assigning the DataTable as Data Source of the DropDownList. Now the user can select a column and update the column's value. But one problem may arise, while updating the list; if the user does not have permission to update then it will generate an error. This can be solved by impersonating the Sharepoint\system user by using the SPSecurity.RunWithElevatedPrivileges method. This is a technique to use Object Model with full privilege..

C#
SPSecurity.RunWithElevatedPrivileges(delegate()
{
   using (SPSite ElevatedsiteColl = new SPSite(siteAddress))
   {
      ElevatedsiteColl.AllowUnsafeUpdates = true;
      using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(webName))
      {
         ....
      }
   }
});

Inbetween this method, the code will execute with an Administrative (Sharepoint\system) privilege. So we will be able to do any kind of read/update/delete operation.

In the code you may also have noticed that I am initiating SPSite, SPWeb (those are created repeatedly) in a using block. This is because SPSite and SPWeb is a very resource consuming object. So we have to make sure the instance gets disposed after its use.

Conclusion

I kept the source as simple as possible. Still, if someone feels that some areas needed more explanatione, you are welcome to ask me to do so.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Gen-i, Australia
Australia Australia
Expertise area: ASP.NET, C#, Microsoft Office SharePoint 2010 & 2007, Web Service, Windows-based Applications etc.

Blog: http://mydevdiary.blogspot.com/

Comments and Discussions

 
GeneralReferences in project Pin
AACINC16-Feb-09 6:10
AACINC16-Feb-09 6:10 
GeneralRe: References in project Pin
Anupam Ranku16-Feb-09 15:52
Anupam Ranku16-Feb-09 15:52 
GeneralRe: References in project Pin
AACINC17-Feb-09 4:13
AACINC17-Feb-09 4:13 
GeneralI do get an error at the IDE Pin
Rainman_the_first14-Nov-08 1:42
Rainman_the_first14-Nov-08 1:42 
Generalproject cannot run Pin
DavidSmithgf22-Jul-08 21:05
DavidSmithgf22-Jul-08 21:05 
GeneralRe: project cannot run Pin
Anupam Ranku22-Jul-08 22:23
Anupam Ranku22-Jul-08 22:23 
GeneralRe: project cannot run Pin
DavidSmithgf23-Jul-08 15:04
DavidSmithgf23-Jul-08 15:04 
AnswerRe: project cannot run Pin
Anupam Ranku23-Jul-08 17:32
Anupam Ranku23-Jul-08 17:32 
GeneralRe: project cannot run Pin
DavidSmithgf23-Jul-08 17:43
DavidSmithgf23-Jul-08 17:43 
GeneralRe: project cannot run Pin
Anupam Ranku23-Jul-08 17:53
Anupam Ranku23-Jul-08 17:53 
QuestionI cant get the update working! help Pin
AlexanderTheGreat0716-Jul-08 4:25
AlexanderTheGreat0716-Jul-08 4:25 
AnswerRe: I cant get the update working! help Pin
Anupam Ranku19-Jul-08 19:08
Anupam Ranku19-Jul-08 19:08 
GeneralSharepoint References Pin
DABBee15-Jun-08 18:51
DABBee15-Jun-08 18:51 
AnswerRe: Sharepoint References Pin
Anupam Ranku15-Jun-08 20:45
Anupam Ranku15-Jun-08 20:45 
I have not seen anyone to face the problem you have mentioned. Because if you have SharePoint installed then the Microsoft.SharePoint.Publishing.dll should be located under
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\ directory.
So I think, you can recheck with your installation. And make sure that DLL didn’t get deleted mistakenly.
If you still can not figure out what is the problem then feel free to knock me again with your details installed information.
QuestionError with SPSite Pin
dudin25-Apr-08 10:19
dudin25-Apr-08 10:19 
GeneralRe: Error with SPSite Pin
Anupam Ranku28-Apr-08 18:59
Anupam Ranku28-Apr-08 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.