Monday, 11 February 2013

SharePoint Form Control People Picker Has Problems Resolving User Value

This latest issue is exactly what it says on the tin :) You have a People Picker control which is used to story a value that corresponds to an SPFieldUserValue in a SharePoint list object. Because there is no instant conversion between the value in the People Picker you perform a conversion:

string userValue = userCtrl.Value.ToString();
int pos = userValue.IndexOf("#");
userValue = userValue.Substring(pos + 1);

(there may well be more elegant ways to get the value out of the People Picker, by all means go out and find them.)

This should extract the part of the uservalue that reads "DOMAIN\username_s" which is what we need to create the SPUser object

SPUser user =   web.EnsureUser(userValue);

I run this for user X and it works perfectly. All good. Then another tester notes that when she enters user Y and clicks Submit, the page doesn't update properly. After a bit of fiddling I discover that in the case of this particular user, the Value property on the People Picker control returns a number, that is, the SharePoint user ID. It is not possible to use this as a parameter for the EnsureUser method, so we are in error city.

So naturally I wondered, why is it behaving for user X and not for user Y. Did a bit of googling and came up with this. It appears to have something to do with the user list in SharePoint. So I went into that user, clicked the Edit User and saved without changing anything. Reran the code. Still had the same problem.

This is how I fixed it in the code - but I still don't know why it didn't work for that user but did for every single other user I tried. So any suggestions on that front welcome.

  int SPId;
 SPUser managerUser = null;
 if (Int32.TryParse(managerValue.Trim(), out SPId) == true)
        managerUser = web.SiteUsers.GetByID(SPId);
        managerUser = web.EnsureUser(managerValue);

Tuesday, 5 February 2013

For Those Wishing to Enable / Disable / Set Emails for Site Access Requests in an entire Site Collection

might I recommend this script from Michael Allen, which does the job with aplomb!

It's Always OK To Refuse To Entertain Trolls

A brief pause from the usual outpourings of code and blather to talk about a serious topic.

A few people in an online SharePoint comm to which I was recently invited expressed mild disagreement with my last post. Probably because it is safer to use JQuery and easier to maintain for administrators. The post merits debate and criticism, I agree. It could have been an opportunity for discussion. But.

One person trolled me. He was negative, provocative and told me I did not know what I was talking about. I told him that I would not entertain responses that were uncivil, as his was. After all, I didn't know this man from Adam and he had never responded to any of my posts before (presumably I had not yet done anything to give him excuse to find fault, so affording him little pleasure.)

In response I got a big pile of rage. The gist of same being that I needed to learn humility and take criticism.

One tiny problem: I cannot take criticism where none has been given.

Wednesday, 23 January 2013

Customising the NewForm.aspx of a List using Server Side Code

Edit - I am getting some feedback from other devs the sum of which is "forget this and do it via jQuery instead".  It is worth noting that this is NOT the recommended way of customising a NewForm or EditForm aspx page, so proceed at your own risk. JQuery is downloadable for free at When I figure out how to use it I will post a follow up!

I've been meaning to write this for ages. The first thing I should mention is that you're not meant to do this. Believe me I have fought the Sharepoint markup generator every step of the way on this one. It wants you to use JQuery and the ECMA client script. But god JQuery is a pain and I don't know it. And it's hard to read or pick up, and dammit I want to use C flippin sharp and nothing and no-one is going to stop me.

OK. As we say in Ireland when someone asks for directions: "If I were you, I wouldn't start from here." But if you DO start from here...

Firstly, you aren't allowed to run code on aspx pages that run in SharePoint. So you have to go into the web config, go to the <PageParserPaths> section and enter something like the following:
<PageParserPath VirtualPath="http://myserver/mySiteColl/MyList/MyForm.aspx" CompilationMode="Always" AllowServerSideScript="true" />

More info on Page Parser Paths here...

OK run an iisreset and that will allow you to customise the NewForm.aspx. Microsoft do not recommend fiddling with the form itself as if you break it, you break the list. So fire up SharePoint Designer to create a new page.

More info on customising the page here

Now I'm aware that there's this nifty plugin called InfoPath which will do the forms for you. Do we have that in my workplace? Not a chance. Not on the cards until we upgrade. So just in case you ask me "what about infopath", that's the answer.

Right. I do this in SharePoint designer. Now that the MyForm.aspx has been created, click the "Code" tab. But before we continue it is important to note the following:

None of the standard SharePoint OK or Cancel buttons is customisable.

Trust me. I tried. Your best bet is hiding and creating a custom HTML command button of your own. Then you can have runat=server, onclick = "DoTheWork" etc. Then, of course, you have to create a script to do the work :)

So, right up the top of your HTML, put up the following. Note that

(1) when getting the list name, I am using the request parameter and ascertaining the position of the string "MyForm". This means I can re-use the code in other lists if I want.

(2) The ContentPlaceHolder bit of code is how I get to "talk" to the sharepoint form controls through the code. Trying to figure out where the controls are and what they're called and how to get the damn data out of them is like breaking into Fort Knox. So if I want to save back to the list (remember, there is no way to get to the save routine via the OK button BECAUSE I HAVE TRIED)

I need to go right into the markup, grab the guid of the data form web part and then furthermore pull out the name from the the properties tab on the left in SP Designer (it's always ff_number) and then get the value. And don't talk to me about radio buttons. I can't get it to work with radio buttons.

I've done a uservalue control because it's the trickiest.

(3) The ID at the end of the request string will always be the ID on the list item you're looking for, so I used that to retrieve the item.

(4) All saving of items is done through the SPListItem object and the Update() method

(5) I don't have a sharepoint environment and compiler with me right now, so chances are there are a couple of errors in the script, but nothing major. Code below the jump - enjoy!