Server Object Model in Sharepoint 2010
1) Using Server-Object-Model applictaions
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
}
}
}
Get ALL WebApplictaion ContentDatabases in a FARM
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
foreach(SPContentDatabase cdb in webApp.ContentDatabases)
{
Console.WriteLine(cdb.DatabaseConnectionString);
Console.WriteLine(cdb.Name);
}
}
}
Get ALL SiteCollections in an WebApplictaion
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
foreach(SPSite site in webApp.Sites)
{
Console.WriteLine(site.Url);
}
}
}
}
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
Console.WriteLine(web.WebTemplate);
Console.WriteLine(web.WebTemplateId);
each site collection comes with top/root level site.
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
foreach (SPList list in web.Lists)
{
Console.WriteLine(list.Title);
}
Get All DOCUMENT LIBRARIES from root/top level site
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
foreach (SPList list in web.Lists)
{
SPDocumentLibrary doclibrary = list as SPDocumentLibrary;
if (doclibrary != null)
Console.WriteLine(list.Title);
}
System.IO.FileStream fs = new System.IO.FileStream(@"C:\salesdata\salesdata.xlsx", System.IO.FileMode.Open);
byte[]bytes=new byte[fs.Length];
int numbytestoread = (int)fs.Length;
int numbytesread = 0;
while (numbytestoread > 0)
{
int n = fs.Read(bytes, numbytesread, numbytestoread);
numbytesread += n;
numbytestoread -= n;
if (n == 0) break;
}
2) add to document library by giving url + bytes read from step 1
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPDocumentLibrary
docLibrary =web.Lists["Shared Documents"] as SPDocumentLibrary;
docLibrary.RootFolder.Files.Add("http://sp2010:21016/Shared%20Documents/excel1.xlsx", bytes);
docLibrary.Update();
fs.Close();
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPList oldList=web.Lists.TryGetList("Product-custom-list");
if (oldList != null)//delete list if already exists
{
oldList.Delete();
}
Guid newList= web.Lists.Add("Product-custom-list", "Custom List desc", SPListTemplateType.GenericList);
SPList list =web.Lists[newList];
SPFieldCollection fieldCollection=list.Fields;
//add product,amount,qty,total fields
fieldCollection.Add("Amount", SPFieldType.Number, false);
fieldCollection.Add("Qty", SPFieldType.Number, false);
string newcalField = fieldCollection.Add("Total", SPFieldType.Calculated, false);
SPFieldCalculated strNewField =
(SPFieldCalculated)fieldCollection[newcalField];
strNewField.Formula = "=[Amount]*[Qty]";
strNewField.Update();
fieldCollection.Add("Product", SPFieldType.Text, true);
list.OnQuickLaunch = true; //add to quick launch
list.Update(); //update the list first then default view as shown below.
// once list is updated with fields, get default view, add those fields.
SPView view =list.Views["All Items"];
view.ViewFields.Add("Product");
view.ViewFields.Add("Amount");
view.ViewFields.Add("Qty");
view.ViewFields.Add("Total");
view.Update();
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPDocumentLibrary prodDocLib = web.Lists.TryGetList("Product-Specification-doclib") as SPDocumentLibrary;
if (prodDocLib != null)
{
Console.WriteLine("DOcument library already exists");
return;
}
SPListTemplate listTemplate = null;
foreach (SPListTemplate listTemp in web.ListTemplates)
{
if (listTemp.Type == SPListTemplateType.DocumentLibrary)
{
Console.WriteLine(listTemp.Type + "\t-->" + listTemp.Name);
listTemplate = listTemp;
break;
}
}
foreach (SPDocTemplate docTemp in web.DocTemplates)
{
Console.WriteLine(docTemp.Name + "\t\t\t--->" + docTemp.Type);
if (docTemp.Type == 122)
{
Guid guid = web.Lists.Add("Prd specification",
"Prduct Specification Documents",
listTemplate, docTemp);
SPDocumentLibrary prdLibrary=web.Lists[guid] as SPDocumentLibrary;
prdLibrary.OnQuickLaunch = true;
prdLibrary.Update();
break;
}
}
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPList list=web.Lists.TryGetList("Sales-Data");
list.ContentTypesEnabled = true; //enable content type first
list.Update();
SPContentType salesct = web.ContentTypes["SalesCT1"];
list.ContentTypes.Add(salesct);//add content type
list.Update(); //update list
1) Using Server-Object-Model applictaions
- Create Project Console Applictaions/windows farm on server side.
- use .net framerwork 3.5 only
- set build -->platform target to x64.(because 64-bit version)
- Add Microsoft.SharePoint reference to the project
- That's it , ready to write applications using Sharepoint 2010 Server Object Model.
Server Object Model API runs on sharepoint server, accesses resources in the sharepoint server. What user can do in Browser and SharePoint designer,they can do with these API.
It is weakly typed objects.
Server-Object-Model with LINQ will give Strongly typed objects.
Server-Object-Model with LINQ will give Strongly typed objects.
Get All Servers in a FARM
SPFarm farm = SPFarm.Local;
foreach (SPServer server in farm.Servers)
{
Console.WriteLine(server.Address);
Console.WriteLine(server.DisplayName);
Console.WriteLine(server.Name);
Console.WriteLine( server.TypeName);
Console.WriteLine(server.Version);
Console.WriteLine(server.DisplayName);
Console.WriteLine(server.Name);
Console.WriteLine( server.TypeName);
Console.WriteLine(server.Version);
}
Get ALL Services in a FARM
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
Console.WriteLine(service.DisplayName);
Console.WriteLine(service.Instances.Count);
Console.WriteLine(service.Name);
Console.WriteLine(service.TypeName);
Console.WriteLine(service.Version);
foreach(SPServiceApplication serviceApp in service.Applications)
{
Console.WriteLine(serviceApp.DisplayName);
Console.WriteLine(serviceApp.TypeName);
}
}
Get ALL WebApplications in a FARM
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
}
}
}
Get ALL WebApplictaion ContentDatabases in a FARM
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
foreach(SPContentDatabase cdb in webApp.ContentDatabases)
{
Console.WriteLine(cdb.DatabaseConnectionString);
Console.WriteLine(cdb.Name);
}
}
}
Get ALL SiteCollections in an WebApplictaion
SPFarm farm = SPFarm.Local;
foreach (SPService service in farm.Services)
{
if (service is SPWebService)
{
SPWebService webService = service as SPWebService;
foreach(SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.DisplayName);
foreach(SPSite site in webApp.Sites)
{
Console.WriteLine(site.Url);
}
}
}
}
Get ALL Sites in SiteCollections
String siteCollectionUrl="http://sp2010:21016";
SPSite site = new SPSite(siteCollectionUrl);
foreach(SPWeb web in site.AllWebs)
{
Console.WriteLine(web.Title);
Console.WriteLine(web.Url);
}
Update site title
SPSite site = new SPSite("http://sp2010:21016");
SPWeb rootweb =site.RootWeb;
rootweb.Title = "Sales data";
rootweb.Update();
Delete site from SiteCollection
SPSite site = new SPSite("http://sp2010:21016");
SPWeb rootweb = site.AllWebs["Sales"]; //deleting sales site.
rootweb.Delete();
Find Site Template
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
Console.WriteLine(web.WebTemplate);
Console.WriteLine(web.WebTemplateId);
Get All lists from root/top level site
each site collection comes with top/root level site.
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
foreach (SPList list in web.Lists)
{
Console.WriteLine(list.Title);
}
Get All DOCUMENT LIBRARIES from root/top level site
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
foreach (SPList list in web.Lists)
{
SPDocumentLibrary doclibrary = list as SPDocumentLibrary;
if (doclibrary != null)
Console.WriteLine(list.Title);
}
Upload a excel document to Shared Document Library in root/top level site
1) open file & construct byte arraySystem.IO.FileStream fs = new System.IO.FileStream(@"C:\salesdata\salesdata.xlsx", System.IO.FileMode.Open);
byte[]bytes=new byte[fs.Length];
int numbytestoread = (int)fs.Length;
int numbytesread = 0;
while (numbytestoread > 0)
{
int n = fs.Read(bytes, numbytesread, numbytestoread);
numbytesread += n;
numbytestoread -= n;
if (n == 0) break;
}
2) add to document library by giving url + bytes read from step 1
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPDocumentLibrary
docLibrary =web.Lists["Shared Documents"] as SPDocumentLibrary;
docLibrary.RootFolder.Files.Add("http://sp2010:21016/Shared%20Documents/excel1.xlsx", bytes);
docLibrary.Update();
fs.Close();
Create a customList in root/top level site
1) Create custom list with
columns
1) Product text field
2) Amount number
3) Qty number
4) Total calculated field.
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPList oldList=web.Lists.TryGetList("Product-custom-list");
if (oldList != null)//delete list if already exists
{
oldList.Delete();
}
Guid newList= web.Lists.Add("Product-custom-list", "Custom List desc", SPListTemplateType.GenericList);
SPList list =web.Lists[newList];
SPFieldCollection fieldCollection=list.Fields;
//add product,amount,qty,total fields
fieldCollection.Add("Amount", SPFieldType.Number, false);
fieldCollection.Add("Qty", SPFieldType.Number, false);
string newcalField = fieldCollection.Add("Total", SPFieldType.Calculated, false);
SPFieldCalculated strNewField =
(SPFieldCalculated)fieldCollection[newcalField];
strNewField.Formula = "=[Amount]*[Qty]";
strNewField.Update();
fieldCollection.Add("Product", SPFieldType.Text, true);
list.OnQuickLaunch = true; //add to quick launch
list.Update(); //update the list first then default view as shown below.
// once list is updated with fields, get default view, add those fields.
SPView view =list.Views["All Items"];
view.ViewFields.Add("Product");
view.ViewFields.Add("Amount");
view.ViewFields.Add("Qty");
view.ViewFields.Add("Total");
view.Update();
Create Excel template Document Library in root/top level site
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPDocumentLibrary prodDocLib = web.Lists.TryGetList("Product-Specification-doclib") as SPDocumentLibrary;
if (prodDocLib != null)
{
Console.WriteLine("DOcument library already exists");
return;
}
SPListTemplate listTemplate = null;
foreach (SPListTemplate listTemp in web.ListTemplates)
{
if (listTemp.Type == SPListTemplateType.DocumentLibrary)
{
Console.WriteLine(listTemp.Type + "\t-->" + listTemp.Name);
listTemplate = listTemp;
break;
}
}
foreach (SPDocTemplate docTemp in web.DocTemplates)
{
Console.WriteLine(docTemp.Name + "\t\t\t--->" + docTemp.Type);
if (docTemp.Type == 122)
{
Guid guid = web.Lists.Add("Prd specification",
"Prduct Specification Documents",
listTemplate, docTemp);
SPDocumentLibrary prdLibrary=web.Lists[guid] as SPDocumentLibrary;
prdLibrary.OnQuickLaunch = true;
prdLibrary.Update();
break;
}
}
Create content type programatically
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
//SPWeb web = site.OpenWeb();
//foreach (SPContentType ct in web.ContentTypes)
//{
// Console.WriteLine(ct.Name+"\t-->"+ct.Group);
//}
//return;
SPContentTypeId contentTypeID = SPBuiltInContentTypeId.Item;
SPContentType cType = new SPContentType(web.ContentTypes[contentTypeID],
web.ContentTypes, "SalesCT1");
cType.Description = "Sales data content type";
cType.Group = "Company-Sales-Data";
cType.Sealed = true;
cType.Hidden = false;
web.ContentTypes.Add(cType);
//adding 4 fields 1.sales man 2.yr 2005,2006,2007
string fieldname=web.Fields.Add("Sales Man", SPFieldType.Text, false);
//first add to web field collection then link to content type.
SPField field= web.Fields.GetField(fieldname);
SPFieldLink fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2005", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2006", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2007", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
cType.Update();
Add content type to a List programatically in sharepoint 2010
SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
SPList list=web.Lists.TryGetList("Sales-Data");
list.ContentTypesEnabled = true; //enable content type first
list.Update();
SPContentType salesct = web.ContentTypes["SalesCT1"];
list.ContentTypes.Add(salesct);//add content type
list.Update(); //update list
No comments:
Post a Comment