Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Using SPPageStatusSetter to Show Status SharePoint 2010

Using SPPageStatusSetter to Show Status SharePoint 2010

In this post you will learn how to display a Status of an item ( i.e. if item has been added, updated, deleted or whatever…) in your status bar that renders just below the ribbon and the top navigation menu.

Well, the thought process is to include the SPPageStatusSetter control in a web part and then dynamically adding this web part to list view page from an event receiver along with status bar message. The reason behind this approach is because you cannot get the current page context in event receiver to include SPPageStatusSetter control to any page. In Nutshell, we will create a webpart with the SPPageStatusSetter control and will set the message(for the SPPageStatusSetter via property) in our event receiver.

Create a WebPart – Create an empty SharePoint project. Add a web part item and an event receiver item. Now in the web part code add a custom property and add the SPPageStatusSetter control. The message(status) for the SPPageStatusSetter control will be set through the custom property of web part from event reciever.

Web part Code :

public class StatusBarWebpart : WebPart
{
SPPageStatusSetter statusBar;
string oMessage;

public StatusBarWebpart()
{ }

[Category("Custom Properties")][Browsable(false)]

public string Message
{
get
{
return oMessage;
}
set
{
oMessage = value;
}
}

protected override void CreateChildControls()
{
statusBar = new SPPageStatusSetter();
statusBar.AddStatus(“Action”, Message, SPPageStatusColor.Blue);
Controls.Add(statusBar);
}

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(“Status Bar demo”);
RenderChildren(writer);
}
}

Now we will add

Now add the custom web part which contains the SPPageStatusSetter control dynamically in the events and make sure that you change the status of the SPPageStatusSetter control in every event through the custom property of the web part.

public class EventRecieverStatus : SPItemEventReceiver
{
///
/// An item is being added.
///

StatusBarWebpart.StatusBarWebpart owp;

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPWeb web = properties.Web;
string oUrl = properties.List.DefaultViewUrl;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager coll = web.GetLimitedWebPartManager(oUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

if (coll.WebParts.Count > 1)
{
owp = (StatusBarWebpart.StatusBarWebpart)coll.WebParts[1];
if (owp != null)
{
owp.Message = “Item Added”;
coll.SaveChanges(owp);
}
}
else
{
owp = new StatusBarWebpart.StatusBarWebpart();
owp.Message = “Item Added”;
owp.Hidden = true;
coll.AddWebPart(owp, “Left”, 1);
coll.SaveChanges(owp);
}
}
///
/// An item is being updated.
///

public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
SPWeb web = properties.Web;
string oUrl = properties.List.DefaultViewUrl;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager coll = web.GetLimitedWebPartManager(oUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

if (coll.WebParts.Count > 1)
{
owp = (StatusBarWebpart.StatusBarWebpart)coll.WebParts[1];
if (owp != null)
{
owp.Message = “Item Updated”;
coll.SaveChanges(owp);
}
}
else
{
owp = new StatusBarWebpart.StatusBarWebpart();
owp.Message = “Item Updated”;
owp.Hidden = true;
coll.AddWebPart(owp, “Left”, 1);
coll.SaveChanges(owp);
}
}
}

No comments:

Post a Comment