Search This Blog

Wednesday 30 May 2012

SharePoint Web application, site collection, site operations through Object Model


   1.  Gets the collection of all site collection in the web application
a.  Approach 1
                        i.   SPContext.Current.Site.WebApplication.Sites

It returns the SPSiteCollection, we can loop through the sites in this collection.
b.  Approach 2
                        i.   SPWebApplication webapplication = SPWebApplication.Lookup(new Uri(SPContext.Current.Site.Url));
foreach (SPSite wsite in webapplication.Sites)

This approach we get the SPWebApplication, we can loop through the sites through webapplication.Sites

   2.  Gets the collection of all web sites that are contained within the site collection, including the top-level site and its subsites
a.  Get the spsite object
                        i.   SPSite osite = new SPSite(SPContext.Current.Site.ID)

The SPSite object represents a collection of site in a web application including top-level site.

b.  Get the collection of all web sites
                        i.   SPWebCollection oWebSites = osite.AllWebs

It returns SPWebCollection, which gets the collection of all web sites in the site collection, including the top-level site

   3.  Get the root web site of the site collection
a.  SPWeb rootWeb = osite.RootWeb

b.  Verify whether a web is root web
                        i.   foreach (SPWeb web in wsite.AllWebs)
                {
                    if (web.IsRootWeb) //IsRootWeb returns a Boolean value indicates whether the web is root web or not
                    {
                          
                           }      
  }
   4.  Get the parent web site of the specified web
a.  Spweb.ParentWeb
It returns the SPWeb.     
Get User Access Details

   1.  Get Sub web site of the Current User
a.  Method – GetSubWebsForCurrentUser()
It returns the collection of subsites beneath the current site of which the current user is a member

   2.  Verify current user is a Site Admin
a.  Spweb.CurrentUser.IsSiteAdmin
It returns a Boolean value, which specify whether the current user is a site admin or not

   3.  Gets collection of groups, which user is a member
a.  Spweb.CurrentUser.Groups
It returns a SPGroupCollection, in which user is a member.

   4.  Get the collection of group that the user owns
a.  Spweb.CurrentUser.OwnedGroups
It returns a SPGroupCollection, in which user is a member.

   5.  Check current user is a member of a specified group
a.  SPGroup ogrp = web.Groups["Owners"];                    web.IsCurrentUserMemberOfGroup(ogrp.ID);
It returns a Boolean value, which indicate whether the current user is a member of the specified group

Refer the below code, where I have worked out above mentioned operations

Code Snippet:


















Output:























Hope, this help guys, who new to SharePoint object model

No comments:

Post a Comment