Wednesday, December 10, 2008

Creating a sharepoint page dynamically.

The SPWeb object for a site exposes a Files property with a public Add method that allows you to add new site pages. There is an overloaded version of the Add method that allows you pass a stream object with the content of the new page. The following example demonstrates writing the contents of a new page to a Memory Stream object and then using it to create a new site page named Hello.htm.

//write out new page in memory stream.
MemoryStream stream=new MemoryStream();
StreamWriter writer=new StreamWriter(stream);
writer.WriteLine("");
writer.WriteLine("Hello, world");
writer.WriteLine("");
writer.Flush();

//add new page to site
SPWeb site=SPContext.Current.Web;
site.Files.Add("hello.htm",stream);



Let me know if you have any question.

Thanks,
Sanjay

Friday, December 5, 2008

How to create a custom e-mail alert handler in Microsoft Office SharePoint Server

This method creates a class that inherits from the IAlertNotificationHandler interface and that uses the OnNotification method. This method lets you intercept the outgoing e-mail alerts and modify them. You can access most of the properties of the alert. By using XML parsing and SharePoint object model code, you can extract all the information that you must have to modify the e-mail alert. Then, you can build the HTML stub to display the e-mail alert based on your requirements. Also, you can send the e-mail alert by using SharePoint̢۪s SendMail functionality.

public class Class1:IAlertNotifyHandler
{

#region IAlertNotifyHandler Members

public bool OnNotification(SPAlertHandlerParams ahp)
{
try
{
SPSite site = new SPSite(ahp.siteUrl+ahp.webUrl);
SPWeb web = site.OpenWeb();
SPList list=web.Lists[ahp.a.ListID];
SPListItem item = list.GetItemById(ahp.eventData[0].itemId) ;

string FullPath=HttpUtility.UrlPathEncode(ahp.siteUrl+""+ahp.webUrl+""+list.Title+""+item.Name);
string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "" + ahp.webUrl + "" + list.Title);
string webPath=HttpUtility.UrlPathEncode(ahp.siteUrl+""+ahp.webUrl);

string build = "";
if (ahp.eventData[0].eventType==1)
eventType="Added";
else if(ahp.eventData[0].eventType==2)
eventType="Changed";
else if(ahp.eventData[0].eventType==3)
eventType="Deleted";

build = ""+
"

"+ item.Name.ToString() +" has been "+eventType +"

"+
""+
""+
"" +
"
"+
"Modify my Settings
View "+item.Name+"View " + list.Title + "
";
string subject=list.Title.ToString() ;
SPUtility.SendEmail(web,true , false, ahp.headers["to"].ToString(), subject,build);
return false;
}
catch (System.Exception ex)
{
return false;
}
}

#endregion
}

You can put your questions/Suggestions in the comment section, I w ill try to respond as soon as possible.

Thanks,
Sanjay