Friday, November 20, 2009

HttpContext in SPItemEventReceiver constructor doesn't work in ItemDeleting events

A common way of getting the context in an event handler is by setting in the constructor. This is well documented.

However, this doesn't work in Deleting events, only Updating and Adding.
public class CustomEventReceiver : SPItemEventReceiver
{
HttpContext current = null;
public CustomEventReceiver() : base()
{
current = HttpContext.Current;
}
}

Thursday, July 16, 2009

Control rendering of fields in a page layout

To display a field on a page based on a page layout when it's being edited, but not when displayed:

<%if (Teasers11.ControlMode != SPControlMode.Display)
{%>
<SharePointWebControls:CheckBoxChoiceField FieldName="Teasers" runat="server" id="Teasers11"/>
<%} %>

Thursday, May 28, 2009

Getting SPList from full URL of list

Code snippet for a quick and easy way of getting the SPList object from the full path to a SharePoint list:

String listurl = "http://sp:40006/Planets/Mars/Lists/Moonlist/AllItems.aspx";
SPList planetList;

using (SPSite sps = new SPSite(listurl))
{
using (SPWeb spw = sps.AllWebs[sps.ServerRelativeUrl])
{
planetList = spw.GetList(listurl);
}
};

Sunday, March 1, 2009

Creating a EntityEditorWithPicker with custom items

Googling around for information about the SharePoint people picker results in a couple of excellent posts that can be used for the basis of a customised EntityEditorWithPicker.

In particular: Customizing the EntityEditorWithPicker by Igor Kozlov and Customize EntityEditorWithPicker by Hrvoje Kušanić.

I needed to go one step further with my SimpleQueryControl, I needed to filter out users by only selecting those with a value set by a combobox on the page with the picker.

To do this I set the custom propery of the EntityEditorWithPicker, where AppPicker is the ApproverPickerEntityEditor and BusinessAreaPicker is the combobox:


ApproverPickerEntityEditor AppPicker = new ApproverPickerEntityEditor();
AppPicker.CustomProperty = BusinessAreaPicker.SelectedValue;


This CustomProperty is appended to the QueryString which is sent by the open-dialog client side javascript. So to access this we can examine the QueryString in the class that inherits from PickerDialog:


public class ApproverPickerDialog : PickerDialog


In the OnLoad event of this PickerDialog I inserted the following code:


protected override void OnLoad(EventArgs e)
{
ApproverQueryControl thisQC = (ApproverQueryControl)this.QueryControl;
thisQC.SelectedBusinessArea = this.Page.Request.QueryString["CustomProperty"];
base.OnLoad(e);
}


Where ApproverQueryControl is the class inheriting from SimpleQueryControl, it has a property SelectedBusinessArea which is used by the IssueQuery method to filter out users by the value selected in the BusinessAreaPicker on the form page.

Sunday, February 22, 2009

CustomAction added to SiteActions menu

To add a menuitem on to the SiteActions menu to link to the listview of the current pages library:
<customaction id="CustomPagesAdministration" 
groupid="SiteActions"
location="Microsoft.SharePoint.StandardMenu"
sequence="0" title="Pages Administration"
rights="ApproveItems">
<urlaction url="Pages/Forms/AllItems.aspx">
</urlaction>
</customaction>
This will only show up for users that have "Approve" rights on the pages library.


Tuesday, January 6, 2009

Title Field not being copied over to Records Center

For some reason when sending a document to a Records Center the "Title" field does not populate. Googling this gives little joy, though a couple of results announce that this is a bug in SharePoint. To get around this problem do the following:

Modify view in the destination Document Library, add the "Title" field [optional]
In the Document Library settings, columns section change the "Title" field's name to something else, eg: "Title_OLD"
In the Document Library settings, add a new column called "Title" and set its "required" setting to "Yes"

This hasn't been extensively tested, so be careful to monitor the results.

Tuesday, December 9, 2008

Allow multiple values in a List Lookup SPField

To create a new field which can select multiple options from a lookup list:
w.Fields.AddLookup(FieldName, LookupList.ID, false);
w.Fields[FieldName].Group = "My Columns";
SPFieldLookup f = (SPFieldLookup)w.Fields[FieldName];
f.AllowMultipleValues = true;
w.Fields[FieldName].Update(true);
Where:
SPWeb w is the web site to store the new column in
String FieldName is to be the new field's Name
SPList LookupList is the List containing the lookup listitems