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!

Friday, 19 October 2012

I Deleted My Last Post About Dynamically Rendering Controls From a SharePoint List

because I was stupid enough to forget that it is impossible, without a lot of fuss and bother, to add event handing to controls which are only rendered at runtime. I'll just take my dunce hat and sit in the corner.

It was not a total waste of time, however, as I was able to use the looping code and Response.Write to write the markup to a text file and then stick it back into the ASPX page. But as they say when looking for directions in Ireland, my home country, "well I wouldn't start from here if I were you."

Sigh. The loop DOES work well when submitting to list and that's coming up shortly.

Monday, 1 October 2012

Oi! My Content Editor Web Part is Borked!

Had an issue today where I got an email saying "ever had this error before?" It was a Content Editor Web Part (those thingies that allow you to add fancy text to a SharePoint page via a web part) that was causing the browser to throw errors. The browser was apparently failing to recognise the background colour or some other bit of javascript. Also the context menu that dropped down from the "edit" command on the web part - viewable when you select "Edit Page" - was not dropping down. The whole thing seemed borked.

I tried adding in a few other Content Editor Web Parts and for the most part they broke in the same way. I closed them using the Web Parts Maintenance Page (which is like the regular page only you add ?contents=1 to the URL) and then had a look at the original web part. Clicking Source Text Editor, I noticed the following.
<DIV blah blah blah piles of attributes that looked as if they'd been lifted out of MS Word etc etc>

Since the DIV contained no text or formatting, I deleted it and refreshed the web part. Hey presto, the borkedness was fixed. The tags were interfering with the HTML. Interesting indeed if this is being caused by SharePoint's very own Rich Text Editor!

Thursday, 27 September 2012

Help! I can't create a Site Collection without getting "Access Denied" errors!

Another one of those problems which is a short post but which took me hours upon hours to fix. The error is as follows:

You log into SharePoint Central Admin with the intent of creating a site collection. You go to Applications and do the necessary, ensuring it is added to the correct Web Application yada yada yada. And then you go to the new link for your site collection - because you want to create some sites in it, naturally enough.

Access Denied Error!

Butbutbut - I'm the Farm Administrator! How can this be? How can I not see my own damn site collections?

By the time I had found the solution, via the good offices of google, I was quite ready to be the Funny Farm Administrator. But never mind. I know now what it is.

1. Central Admin. Attempt to access SharedServices1 page. Chances are you will get an Access Denied error

2. Application Management - Policy for Web Application. Is your farm account listed there with Full Control? If not, add it for the relevant Web Application (you'll see the web app filter in the view dropdown on the top right)

3. Still having problem? Go to Operations - Service Accounts. BE VERY CAREFUL ABOUT DOING THIS ON A PRODUCTION SERVER, HAVE A BACKUP FIRST:
Click "Web application pool" option button
Select Web Service - Winds SharePoint Services Web Application
Select Application Pool - usually only Sharepoint - 80 available. That's fine.
Select "Configurable" radio button and enter farm admin account user and password
Click OK
A lot of the links in SharedServices will still be broken but you will be able to view SharedServices1 and more importantly your site collections!