Monday, September 3, 2012

SP 2010 Start up Simple Console App


SharePoint 2010 Start up Simple Console App

Start VS2010, and follow the steps  to write your first console application in SharePoint 2010. The basic aim of this post is to familiar with the Object Model SharePoint uses i.e.SPFarm, SPWebApplication, SPSite, SPWeb, SPList etc.
  •  On your SharePoint development machine, create a new Console Application in Visual Studio 2010. Make sure your target framework is .NET 3.5. This is because SharePoint 2010 is built on .NET 3.5 SP1.
  • Add a reference to Microsoft.SharePoint.dll. You will find this reference under the .NET tab of the Add References Dialog within Visual Studio. Look for Microsoft.SharePoint.
  • Use code snippet shown below.
Note: Please change the url ( “http://sp-pc”)  with your own,  before running the code snippet given below.
Complete Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace SharepointConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite(“http://sp-pc”))
{
Console.WriteLine(” Site Properties :: Title ” + site.RootWeb.Title);
Console.WriteLine(” Site Properties :: site.AverageResourceUsage  ” + site.AverageResourceUsage);
Console.WriteLine(” Site Properties :: site.ContentDatabase.DatabaseConnectionString   ” + site.ContentDatabase.DatabaseConnectionString);
Console.WriteLine(” Site Properties :: site.HostName   ” + site.HostName);
Console.WriteLine(” Site Properties :: site.ID (GUID)  ” + site.ID);
Console.WriteLine(” Site Properties :: site.Owner.LoginName   ” + site.Owner.LoginName);
Console.WriteLine(” Site Properties :: site.Owner.Name   ” + site.Owner.Name );
Console.WriteLine(” Site Properties :: site.Owner.IsSiteAdmin    ” + site.Owner.IsSiteAdmin);
Console.WriteLine(” Site Properties :: site.Owner.Sid     ” + site.Owner.Sid);
Console.WriteLine(” Site Properties :: site.Owner.Xml     ” + site.Owner.Xml);
Console.WriteLine(” Site Properties :: site.Port  ” + site.Port);
Console.WriteLine(” Site Properties :: site.PortalName   ” + site.PortalName);
Console.WriteLine(” Site Properties :: site.PortalUrl    ” + site.PortalUrl);
Console.WriteLine(” Site Properties :: site.Protocol   ” + site.Protocol);
Console.WriteLine(” Site Properties :: site.ServerRelativeUrl   ” + site.ServerRelativeUrl);
Console.WriteLine(” Site Properties :: site.SystemAccount.LoginName   ” + site.SystemAccount.LoginName);
Console.WriteLine(” Site Properties :: site.Url   ” + site.Url);
Console.WriteLine(” Site Properties :: site.WebApplication.DisplayName  ” + site.WebApplication.DisplayName);
Console.WriteLine(” Site Properties :: site.WebApplication.Id   ” + site.WebApplication.Id);
Console.WriteLine(” Site Properties :: site.WebApplication.Name    ” + site.WebApplication.Name);
int counter = 0;
foreach(string name in  site.WebApplication.Sites.Names)
Console.WriteLine(” Site Properties :: site.WebApplication.Sites.Names[" + counter +"]    ” + name );
Console.WriteLine(” Site Properties :: site.WebApplication.Farm.DisplayName    ” + site.WebApplication.Farm.DisplayName);
Console.WriteLine(” Site Properties :: site.WebApplication.Farm.Id (GUID)    ” + site.WebApplication.Farm.Id);
Console.WriteLine(” Site Properties :: site.WebApplication.Farm..Name     ” + site.WebApplication.Farm.Name);
Console.WriteLine(” Site Properties :: site.WebApplication.Farm..Name     ” + site.WebApplication.Farm.Servers.FirstOrDefault() );
}
Console.ReadLine();
}
}
}



Output:

Site Properties :: Title Home
Site Properties :: site.AverageResourceUsage  0
Site Properties :: site.ContentDatabase.DatabaseConnectionString   Data Source=SP-PC\SharePoint;Initial Catalog=WSS_Content;Integrated Security=True;Enlist=False;Connect Timeout=15
Site Properties :: site.HostName   sp-pc
Site Properties :: site.ID (GUID)  e33c7cae-5cc4-4c46-b634-11f6bf6b1d00
Site Properties :: site.Owner.LoginName   SP-PC\administrator
Site Properties :: site.Owner.Name   SP-PC\administrator
Site Properties :: site.Owner.IsSiteAdmin    True
Site Properties :: site.Owner.Sid   S-1-5-21-1559381635-1802688237-1039693252-500
Site Properties :: site.Owner.Xml     <User ID=”1″ Sid=”S-1-5-21-1559381635-1802688237-1039693252-500″ Name=”SP-PC\administrator” LoginName=”SP-PC\administrator” Email=”someone@example.com” Notes=”" IsSiteAdmin=”True” IsDomainGroup=”False”
Flags=”0″ />
Site Properties :: site.Port  80
Site Properties :: site.PortalName
Site Properties :: site.PortalUrl
Site Properties :: site.Protocol   http:
Site Properties :: site.ServerRelativeUrl   /
Site Properties :: site.SystemAccount.LoginName   SHAREPOINT\system
Site Properties :: site.Url   http://sp-pc
Site Properties :: site.WebApplication.DisplayName  SharePoint – 80
Site Properties :: site.WebApplication.Id   9157d552-44c9-48f3-8a11-1ff9f2493bc1
Site Properties :: site.WebApplication.Name    SharePoint – 80
Site Properties :: site.WebApplication.Sites.Names[0]
Site Properties :: site.WebApplication.Sites.Names[1]    my
Site Properties :: site.WebApplication.Farm.DisplayName    SharePoint_Config_b00efd1d-c9f5-4107-a4ee-29bf2e16da4f
Site Properties :: site.WebApplication.Farm.Id (GUID)
8d12b296-6a80-4c87-a62a-501871befa34
Site Properties :: site.WebApplication.Farm..Name     SharePoint_Config_b00efd1d-c9f5-4107-a4ee-29bf2e16da4f
Site Properties :: site.WebApplication.Farm..Name     SPServer Name=SP-PC

No comments: