Monday, 26 November 2007

SharePoint Users : Adding using C#

We had an issue recently on one of the sites we're currently developing. We had to build a SharePoint 2007 Portal site which used the libraries of Commerce Server 2007, including the Profile and Membership sections. MOSS generally uses the Windows Authentication Provider, which is fine for intranet sites using Windows Domains and the like, but this site is a public-facing eCommerce site so Windows Logins were out of the question

After much ado trying to get the Commerce Server UpmProvider assembly working for both sections of the site (more in another post coming soon!!), we came upon another problem :
How do you add users to a SharePoint application automatically as they sign up to an eCommerce site?

We had the main section of our portal accessible to anonymous users, as is the way with the web. However, when users get to a certain point, like Checkout or Account Management, we decided that we should create Sub sites of the main portal to act as secure areas. Therein lies the problem, as these sites were out of bounds to anonymous users and required a SharePoint login to be able to access the pages within.

So, with the help of Andrew Connell, i was able to write a small section of code which would do stuff that usually only the SharePoint administration site had rights to do : Add users to a SharePoint site programatically. There are a fair few blogs about this on the web but they all require the site to be running under some super-elevated privilege user. Since we're dealing with a public eComm, we simply couldn't have any kind of admin user anywhere near the site so something else needed to be done

a handy little snippet of SharePoint came to the rescue:

SPSecurity.RunWithElevatedPrivileges()

The method take a delegate or a method-pointer which can then be run under SuperAdmin rights :¬)

Here's an example of the full thing so you can get an idea of the syntax for your code


SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb web = new SPSite(mySiteID).OpenWeb();
try
{
SPGroup myViewers = web.Groups["Viewers"];
web.AllowUnsafeUpdates = true;
myViewers.AddUser("upmprovider:" + txtEmailAddress.Text,
txtEmailAddress.Text,
txtEmailAddress.Text,
"User added by " + Environment.MachineName
+ " @ " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
}
catch (SPException exSP)
{
sStep = exSP.Message;
}
catch (Exception exGeneral)
{
sStep = exGeneral.Message;
}
});



Here we add a UpmProvider-centric user, in this case Microsoft.CommerceServer.Runtime.Profiles.UpmMembershipProvider, as a user to the SharePoint Administration site Viewers Group. We had to add the AllowUnsafeUpdates bit to disable the inherent validation enforced by MOSS. Since this was run for a tiny WebPart, validation was handled internally and didn;t need to affect the rest of the site. This should be okay since it will be re-enabled as soon as the delegate has executed. Also, you should use this code sparingly since it throws the whole Security/Authentication sections wide-open for that particulare section

Hope this helps somebody :¬)

Tuesday, 20 November 2007

UK Postcodes

As with a lot of things in sunny(?) England, Postcodes can be a total nightmare to parse. they have allsorts of rules about combinations of letters and numbers.

So, here is a Regular Expression i blagged from here which validates the lot.


Job done :¬)

First Post is always the deepest

yarr, my first blog post ever.

TBH, i've been reaping the niceties of blogs for a good while now. Mainly for programming tips and work-arounds but the occasional random blog post that comes up in google as well. It always very interesting to see what people do to get themselves in or out of certain situations and I find it all quite fascinating

So, i thought it was high-time i gave something back. Kind of like an amalgamation of all the tips i've gotten and added to my work over the years. Hopefully somebody finds it useful :¬)