Software Architecture-Sharepoint Server 2010-First piece of code with SPSite and SPWeb

Environment: Visual Studio 2010 + Sharepoint Server 2010

As part of career direction, I have decided to include Sharepoint as my skillset. So I explored the Sharepoint as end user for the last few weeks. Today I have programmed first piece as sharepoint developer…..Hurrahh..As I had experienced with many other CMS solutions like PHPNuke,Joomla,DotnetNuke and custom build. So basic concept was not new while exploring the sharepoint.

My First Program was targeting to explore the Server Side Object Model specially the class of SPSite which represent the site collection in Sharepoint and SPWeb which target a web site.

      const string siteUrl = "http://win2k8-ultimate/sites/Call";
      using (SPSite siteCollection = new SPSite(siteUrl)) {

          foreach (var item in siteCollection.AllWebs)
          {
              Console.WriteLine("Item of Site collections: " + item.ToString());
          }
        SPWeb site = siteCollection.RootWeb;
        Console.WriteLine("Root Site:" + site.Name + " " + site.PortalName);
          //Gets or sets the description for the site.
        Console.WriteLine("Desc: " + site.Description);
        /// Gets a list of the groups that are associated with the website.
        foreach (var item in site.AssociatedGroups)
        {
            Console.WriteLine(" Related Group: " + item.Name);
        }
        //Gets a collection that contains all the groups in the site collection.
        foreach (var item in site.SiteGroups)
        {
            Console.WriteLine("Sitegroup Name: "+ item.ToString());
        }
        //Gets the users who have been given contribute permissions to the website.
        Console.WriteLine(" Member Group: " + site.AssociatedMemberGroup.Name);
        //Gets or sets the associated owner groups of the website.
        Console.WriteLine(" Owner Group: " + site.AssociatedOwnerGroup.Name);
        //Gets or sets the associated visitor group of the website.
        Console.WriteLine(" Vistor Group: " + site.AssociatedVisitorGroup.Name);

        //Gets the collection of all lists that are contained in the website.
        foreach (SPList list in site.Lists) {
          if (!list.Hidden) {
            Console.WriteLine("Items of List: "+list.Title);
          }

          //Gets the collection of all list definitions and list templates that are available for creating lists on the website.
          foreach (SPListTemplate item in site.ListTemplates)
          {
              Console.WriteLine("Item of List templates: " + item.Name);
          }
            //Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
          foreach (var item in site.AllUsers)
          {
              Console.WriteLine("User of site:( Direct + via Group ) " + item.ToString());
          }
          //Gets the collection of user objects that are explicitly assigned permissions in the website.
          foreach (var item in site.Users)
          {
              Console.WriteLine(" USer of site:(Only Direct users) " + item.ToString());
          }
          //Gets the collection of all users that belong to the site collection.
          foreach (var item in site.SiteUsers)
          {
              Console.WriteLine("user of whole site collection:" + item.ToString());
          }
            //Gets the collection of the website administrators.
          foreach (var item in site.SiteAdministrators)
          {
              Console.WriteLine(" site admins: " + item.ToString() );
          }

            //Gets the collection of Features that are currently activated in the website.
          foreach (var item in site.Features)
          {
              Console.WriteLine("Feature of site: " + item.Definition.DisplayName);
          }
          //  Gets the collection of field objects that represents all the fields in the website.
          foreach (var item in site.Fields)
          {
              Console.WriteLine("feild of site:" + item.ToString());
          }
            //Gets the collection of all files in the root directory of the website.
          foreach (var item in site.Files)
          {
              Console.WriteLine("files of site: "+ item.ToString());
          }
          //Gets the collection of all first-level folders in the website.
          foreach (var item in site.Folders)
          {
              Console.WriteLine("Folders of site:"+ item.ToString() );
          }
            //Gets the collection of modules that are used on the website.
          foreach (var item in site.Modules)
          {
              Console.WriteLine("module of site: " + item.ToString());
          }
        }
        Console.ReadLine();
      }

Comment s explains everything in the above code.
Note:
1. Visual Studio project by default target .Net 4 framework. It needs to be changed to .Net 3.5 framework.
2. Project properties >> Build >> Target plateform needs to be changed to x64.
3. User which is running to code must be site collection administrator, otherwise Access denied exception will be thrown by code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: