Monday, June 24, 2013

SharePoint Server Object Model Hierarchy

In SharePoint,SPSite object also referred to as ‘site’, is actually a site collection object,not a website. SpWeb object also referred to as ‘web’, is actually a single site in the site collection. 
SPWebApplication is the highest level object which has reference to the web application that contains the site collection.

To get the reference to site context in your code, use Microsoft.Sharepoint.SPContext class and its members.
1.       To Reference a site collection:    SPSite oSiteCollection=SPContext.Current.Site;
2.       To Reference a Website:            SPWeb oWeb=SPContext.Current.Web;

The basic SharePoint server object model hierarchy is graphically represented as shown below.  


Thursday, June 20, 2013

Event Receivers - Basics - Item Adding Example

Event Receivers are of two types, Before and After Events. Let me explain with an example, Consider you have a custom list "Employees" in your site which has columns 'Name','salary' and 'Hire Date'. You want to add a 1000$ bonus to employees hired before a year. So, you want to fire an event which basically adds 1000$ to their salary depending on hire date. Now we don't have 'hire date' in the database because it has not been committed yet. so, that is an after property.

Simple Event Receiver which adds 1000$ bonus based on 'Hire Date':
public override void ItemAdding(SPItemEventProperties properties)
        {
            int year = Convert.ToDateTime(properties.AfterProperties["Hire Date"]).Year;
            if (year < DateTime.Today.Year)
            {
                properties.AfterProperties["salary"] = Convert.ToDecimal(properties.AfterProperties["salary"]) + 100;

            }

        }


SharePoint Basic PowerShell Commands

Power Shell:

          Difference between windows powershell and sharepoint powershell: No much difference except that sharepoint powershell is preloaded with “Microsoft.SharePoint.powershell” snapin. If we add this snap in in windows powershell, we can execute sharepoint commands in windows powershell.
To add sharepoint snap in: add-pssnapin “Microsoft.sharepoint.powershell”
To remove the sharepoint snap in: remove-pssnapin “Microsoft.Sharepoint.powershell”
To get all commands in powershell: get-command -pssnapin “Microsoft.Sharepoint.powershell”
To list all web applications: get-spwebapplication
To list all site collection: get-spsite “<url>” ex: get-spsite http://veenar:21607/*   //spsite=>sitecollection
To list all sites in site collection: get-spweb –site http://veenar:21607/                                  //spweb=>sites in site collection
To list Titles of sites in web application: we can pipe the result from one powershell command to another like given below.
 Get-spsite http://veenar:21607/* | get-spweb | select title
To list users who have access to the web application: get-spuser –web “<url>”
To list all features: get-spfeature –limit all

To list all site features: get-spfeature -limit all | where-object {$_.scope -eq "site"}             //scope is site(features are one of the ways of deploying sharepoint solutions to sharepoint-scope can be site,farm,webappliaction)