Working with unique SharePoint 2010 list item security

February 8th, 2010
Share this on Twitter

For about some time I have been working on a SharePoint 2010 solution where I need to heavily customize SharePoint EditItem form. Not a big deal, I created a custom rendering template. I also have a custom Save button that has to perform some additional field setting. One thing that my SPList has to have is a WriteSecurity=2 . WriteSecurity is a new property in SharePoint 2010 allowing your users to create new items in a list but edit ONLY their own.

In my solution I had to respect that notion but I also had to allow administrative users to be able to overwrite this restriction.

So in efforts to do that I created my own custom Save button and overwrote it`s SaveItem() function and elevated permissions on base.SaveItem()

Well, SharePoint didn`t like that idea. The problem was with the fact that item context was transfered from the parent SPWeb which already was opened under the context of the existing user (non-admin) and my list item was convinced that there was no elevation done at the time of saving the item.

One solution that I found working is to create new SPSite, SPWeb objects and get a hold of the list. Once I have an item in question I would pass base.ItemContext.ListItem to it and all of the user entered data would get transfered the new SPListItem object that was now under my control. My save operation succeeded.

Here is how my code looked like:

protected override bool SaveItem()
{
bool saveResult = false;
SPSecurity.RunWithElevatedPrivileges(delegate
{
SPSite site = new SPSite(Constants.LocalSiteCollection);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
web.AllowUnsafeUpdates = true;
SPListItem item = base.ItemContext.ListItem;
/// …. other updates to SPListItem …
item.SystemUpdate();
saveResult = true;
base.RedirectUrl = SPContext.Current.Web.Url;
web.Dispose();
site.Dispose();
});
return saveResult;
}

protected override bool SaveItem()

{

bool saveResult = false;

SPSecurity.RunWithElevatedPrivileges(delegate

{

SPSite site = new SPSite(Constants.LocalSiteCollection);

SPWeb web = site.OpenWeb();

SPList list = web.Lists["MyList"];

web.AllowUnsafeUpdates = true;

SPListItem item = base.ItemContext.ListItem;

/// …. other updates to SPListItem …

item.SystemUpdate();

saveResult = true;

base.RedirectUrl = SPContext.Current.Web.Url;

web.Dispose();

site.Dispose();

});

return saveResult;

}

Enjoy!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , , , , ,

List items validation in SharePoint 2010

February 7th, 2010
Share this on Twitter

In my last article – you have seen how to provision validation formula on individual column in a list or library. Here I’ll show how you can provision validation formula on the list item itself where you can validate multiple columns and display summary message for the user, similar to below:

validation on list

To implement validation you must define the validation section in the schema.xml of the list you’re trying to add validation to. Here is the sample of validation formula and a message in a context of the list definition:

validation definition

<Validation Message=”Something is not quite right with this item”>
      =Title=”My desired value”
</Validation>

The highlighted part can be any formula used in calculated field definition, here is more info on various options to define it.

In case you’re wondering, the same techn ique can be used to provision validation on already existing list by using:

SPList list = SPContext.Current.Web.Lists["MyList"];
if (list!=null)
{
list.ValidationFormula = “=Title=’My desired value’”;
list.ValidationMessage =”Something is not quite right with this item”;
}

 This validation will be triggered every time the new item is created or existing item changed.

Enjoy!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , , ,

Provisioning validation formula to SharePoint 2010 list field

February 6th, 2010
Share this on Twitter

SharePoint 2010 has a really cool feature allowing you to create validation formula on the list field and display a message to a user if validation fails. Something I have been using custom Rendering Templates and Event Handlers in MOSS.

Here we`ll take a look at how e can provision such special fields using VisualStudio 2010 features.

First, here is how our formula was defined in a field settings:

validation formula

Here is the result when the number new item is created:

validation message

In your Visual Studio 2010 you can define the field right in the schema.xml of the list definition where your field will appear.

Here is how something like this would look like:

<Field Type=”Number” DisplayName=”TestNumber” ID=”{e47ae7ad-fb63-4ead-a50c-4143f86e727f}” StaticName=”TestNumber” Name=”TestNumber”>
<Validation Message=”Test Number must be greater than 0″ Script=”function(x){return SP.Exp.Calc.valid(SP.Exp.Node.f(’GT’,[SP.Exp.Node.a(0),SP.Exp.Node.v(0)]),x)}”>
=TestNumber&gt;0
</Validation>
</Field>

The highlighted part defines the formula just like in the UI and pretty much follows calculated field syntax, so you can go pretty complex here.

Good luck!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , ,

Adding SharePoint 2010 rating control on your custom forms

February 5th, 2010
Share this on Twitter

Here is an interesting question from a reader: I’m wondering, is there a way to use SharePoint rating control on a form (custom aspx page, which is in _layouts folder) in SharePoint 2010? If so, what’s the syntax? I found there is a control called RatingScaleField, not sure how to use it.

The answer is: maybe but it would require a lot of engineering around the rating control. Rating control is contextual to the item and it pulls/sends information about users rating to/from the SocialDatabase in SharePoint. If you want to use out of the box rating control on a separate page – it will have to have the context of an item you’re referring to.

One other approach to place out of the box rating control on a custom page is to create a new instance of Display/Edit form rendering template. Meaning that you can arrange your fields in any order you want or add new markup or new fields or ASPX controls to the page. Check out this article to get a general idea how to create your own rendering templates.

Once you have an idea about rendering templates, all of you need to do is to reference your rating field as a CompositeField on a rendering template, like this:

<SharePoint:CompositeField ID=”MyRatingFieldId” runat=’server’ FieldName=’Rating_x0020__x0028_0_x002d_5_x0′ />

This will generate a rating scale field on your form wherever you place it. All the wiring is going to be done as well so SharePoint will know where to send your rating etc.

If you want to know how one of my collegues create his own rating control that utilizes SharePoint 2010 SocialData back end – check out both parts of this article.

Good luck!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , , ,