Pages

Saturday, July 25, 2009

How to Send Email from SharePoint object model by accessing Email Configuration from Central Admin

Here is example code for sending email from SharePoint object model, main part of code would be “SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address”, it will return mail server address which is assigned from SharePoint central administration site as per web application

Sample Code:
,

private void SendEmail()
{
try
{
SmtpClient client = LoadSmtpInformation();
client.Send(BuildMailMessage());
}
catch (Exception Ex)
{
Response.Write(Ex.Message);
}
}

private SmtpClient LoadSmtpInformation()
{

string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
SmtpClient client = new SmtpClient(smtpServer);
client.UseDefaultCredentials = true;
return client;
}

private MailMessage BuildMailMessage()
{

MailMessage message = new MailMessage();

message.From = new MailAddress(SPContext.Current.Web.CurrentUser.Email);
message.To.Add(new MailAddress(User.Email));
message.IsBodyHtml = true;
message.Body = “Email Message”;

message.Subject = "Subject of Email";
return message;

}

Friday, July 24, 2009

How to write error message to SharePoint logs file

SharePoint object model gives us very good functionality and ability to write our customize error messages into SharePoint logs file.

We can pass our error string into LogString method from Microsoft.Office.Server.Diagnostics.PortalLog class and it will write error into logs file.

Sample Code:

try

{

//CODE

}

catch (Exception Ex)
{
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception – {0} – {1} – {2}" , "Sharepoint Application Name" , Ex.Message , Ex.StackTrace);

}

Catch the errors!!!

Thursday, July 23, 2009

SharePoint Terminology and Concepts – Basics of SharePoint

Actually when I have started with SharePoint development, I have many basic questions, like why people are calling 12 folders as 12 Hive, what are content type and many more, so I thought to share with you some basic terminology, concepts and definitions for the same which are using in day to day life while working with SharePoint.

12 Hive- a folder used by SharePoint to share all files that are not stored in the Content and Configuration Database. The Location is LocalDrive:\Program Files\Common Files\Microsoft Shared\web server extensions\12. (Why this location? Since the SharePoint architecture is an extension of IIS, perhaps this is why these files are stored under web server extension.)

CAML - Collaborative Application Markup Language. It is an XML based language which is used to fetch data from SharePoint objects such as lists, document library.

Central Administration – is a special SharePoint website that allows users to perform administrative tasks for the farm. Site is Accessible via Start -->Administrative Tools.

Configuration Database - every SharePoint farm has a single farm wide configuration database that contains configuration data such as which Web Servers are associated with the farm and which users have admin privileges.

Content Database - Typically there is one content database per Web Application. It stores the data associated with that Web application including .aspx pages. As per Microsoft, it’s not feasible or good to write query against database through ADO .NET code or any other way. Only go through the SharePoint Object Model.

Content Type - a reusable type definition that defines the columns and behavior of an item in a list or document library. It can include a set of columns, event handlers, document templates and workflows.

Farm - a set of one or more computers that work together to provide WSS functionality to clients. A simple farm can be a single computer that hosts WSS and SQL. A more complex farm can consists of several front-end Web servers and a dedicated database server.

Feature - allow developers to define site elements and add them to a target site by simply activating the feature. Features can be packaged into solutions and very easy to deploy. They reside in a feature.xml file which uses CAML. Other files are also required such as elements.xml files which define the elements and make up a feature (list definitions, page templates, etc.)

IIS Website- Provides an entry point to IIS and listens for http requests .The default IIS website listens on port 80.

Item - a component of SharePoint site such as the entire farm, a Web Application, or a content database. It seems that an "Item" is defined to be whatever is returned by the -showtree option in the stsadm backup command.

Provisioning - provisioning simply means creating. So provisioning a site collection means creating a site collection.

Site - A storage container for content (such as WebPages), that is always scoped within a site collection. Site content includes lists, documents, and libraries. A site is a foundation for web parts. Also called WSS Site.

Site Collection- a container for WSS Sites also called a WSS Site Collection. Under a web application, the way site collection and sites are organized affects the scope of administrative privileges, security boundaries, and backup and restore operations.

Site Column - a reusable column definition that can be used across multiple lists. A site column defines its name, field type. Default value, formatting and validations. It is then used in lists and document libraries. Later, if the site column definition changes, all lists that use it are also updated.

Site Definition- the top level component in WSS that aggregates smaller definitions to create a site template that can be used to provision sites. To provision a site, a site definition must define one or more configurations. It is site definition's configuration that is actually used to provision a site.

Solution - Solution can be easily deployed. They can contain features and other wss components. It is also known as WSS Solution or .wsp file.

Subsite - informal term that refers to a sub site.

SSP - Shared Service Provider. These are built into SharePoint and include things like Search or Excel Calculation Services.

STSADM - a command line utility, used to perform administrative tasks. STSADM allows users to create site collections, backup and restore operations and more.

Virtual Server-the old name for what is now called a Web Application.

 WebApplication- an IIS website that has been configured to run WSS Sites.(Not a virtual directory).IIS performs authentication , but the SharePoint Web Application performs the authorization.

Web- A top level site was originally called a Root Web.

WorkFlow - Windows Worfklow Foundation, used to attach business logic to list items and documents in a WSS Site. A task list and history can be associated with each workflow. If created using SharePoint Designer, WSS Workflows are difficult to deploy. For professionally developed projects a .NET Solution should be used instead.

WSS Object Model - The object model used to develop SharePoint Applications. The Object model programmatically exposes sites and site Collection. To enable intelligence in Visual Studio you need to copy wss.xsd from 12 hive TEMPLATES folder to c:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\.

To avoid using excessive amounts of memory, use need to call dispose for SPWeb and SPSite objects since they are unmanaged resources.