Thursday, June 5, 2008

SPItemEventReceiver.ItemUpdating + Redirecting to Custom Page

This shows How redirecting to new page inside the SPItemEventReceiver.ItemUpdating event.


class CustomEventHandler: SPItemEventReceiver {

private HttpContext currentContext = null;

public CustomEventHandler()
: base()
{
if (null != HttpContext.Current)
{
currentContext = HttpContext.Current;
}
}

public override void ItemUpdating(SPItemEventProperties properties)
{
SPSite siteColl = new SPSite(properties.SiteId);
SPWeb site = siteColl.OpenWeb(properties.RelativeWebUrl);
SPList list = site.Lists[properties.ListId];

// Add the item and fill it with the values from properties
DisableEventFiring();
SPListItem itemToUpdate = properties.ListItem;
this.CloneItem(itemToUpdate, properties);


itemToUpdate.update();
EnableEventFiring();

SPUtility.Redirect(site.Url + "/ThankYou.aspx",
SPRedirectFlags.Default, currentContext);

site.Dispose();
siteColl.Dispose();
}
}
}

Note : You can only redirect inside the ItemAdding, ItemUpdating,ItemDeleting events only. Becuae in ItemAdded , ItemDeleted and ItemUpdated context is null.

Tuesday, May 27, 2008

Creating SPField Read Only + Only Date format

There are many scenarios that you might need to have read only field which hold only the date. Using below code snippet I was able to create a date type filed which as read only value for end users. 


SPFieldDateTime spDateField = (SPFieldDateTime)sourceList.Fields.CreateNewField(SPFieldType.DateTime.ToString(), "To");
spDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
spDateField.ShowInViewForms = true;
// spDateField.DefaultValue = "[today]"; // This assigns Default value today to datetime
spDateField.ReadOnlyField = true;
sourceList.Fields.Add(spDateField);

sourceList.Update();

Creating and Deleting Hidden Fields in SPList

Creating :
id = sourceList.Fields.Add("SourceRefId", SPFieldType.Guid, false);
SPField field = sourceList.Fields[id];
field.Hidden = true;
field.ReadOnlyField = true;
field.Update();
sourceList.Update();





Deleting
SPField field = sourceList.Fields["SourceRefId"];
field.Hidden = false;
field.ReadOnlyField = false;

field.Update();
sourceList.Fields.Delete("SourceRefId");

Saturday, May 3, 2008

Welcome

Welcome to My WSS 2007 Windows SharePoint Server 2007 Blog.