Blog of an overweight SharePoint addict

Tue 22 May 07

VBA, Excel, SQL, LDAP and the like..

Filed under: Active Directory, Excel, General, LDAP, Programming, Security — Reginald @ 8:25 am

Well, it’s been a very busy week or so, I’ve really been burying my head into the Excel code thats going to form the backend of our application. Due to the nature of the data and the demands of the users, I have certain restrictions on the reference numbers that we’re using to track the items.

To this end, I’ve created a back-end database sitting on the SQL server that Excel references directly using ADO to create new records, query the DB and also run a report or two.

This is now in place and functioning nicely. The next step now is to tidy up the code in Excel, prior to porting it into the full Document Template that the users know and love. The key problem here is going to be the legacy code that exists within the template from a previous owner. (Note to all previous owners: DOCUMENT YOUR DAMN CODE PLEASE!!!!!!)

The next step is also the thorny issue of Authentication. In order to keep the calls to LDAP down to a miniumum, I have created a start-up macro that checks for membership of aparticular AD group before the button that displays the Admin Control panel is made available.

In order to make updating easier when moving from Dev to live, I’ve created a couple of constants to track the LDAP addresses.. These are concatenated within the application to created the full LDAP string.

Public Const LDAP_addy As String = "DC=department, DC=company, DC=com"
Public Const LDAP_SO_financials As String = "CN=grp_SOfinancials, OU=MOSS2007, OU=Service Accounts, OU=Machine Services"

Note: The DSN details above have been changed, but basically represents the domain name department.company.com and then nested OU’s that end with a Security group.

Machine Services
-> Service Accounts
-> Moss 2007
-> grp_SOfinancials

We then check the current logged on username (This is retrieved using advapi32.dll code found on the MSDN website.. e-mail me if you want more details..) against the AD group

database_permitted = IsMemberOf()

database_permitted is then used to hide the Control Panel button on the front page of our Excel worksheet.


Function IsMemberOf() As Boolean
'Routine to check that username is a member of the group passed as a variable
Dim strGrp As IADsGroup
Dim strUser As IADsUser
Dim LDAP_full, SamUser As String
Dim UserSuccess As Boolean
UserSuccess = False

On error goto cleanup

'Connect to the AD using global LDAP string
LDAP_full = "LDAP://" & LDAP_SO_financials & ", " & LDAP_addy

Set strGrp = GetObject(LDAP_full)
For Each strUser In strGrp.Members
SamUser = UCase(strUser.Get("sAMAccountName"))
If Current_User = SamUser Then
UserSuccess = True
End If

Next

'Drop LDAP connection
Set strGrp = Nothing
Set strUser = Nothing

IsMemberOf = UserSuccess

Exit Function
cleanup:

If (Err.Number <> 0) Then
MsgBox ("An error has occured with Active Directory. You're security information cannot be confirmed!" & Err.Number)
End If
Set strGrp = Nothing
Set strUser = Nothing
IsMemberOf = UserSuccess

End Function

Mon 14 May 07

A quiet week for the Blog..

Filed under: Excel, General, SQL — Reginald @ 9:58 am

..but not for me.. I’ve spent the last week working with Excel VBA and OLE to get a connection into our backend SQL server. Then a couple of stored procedures to generate our required indexes and voila, I almost having a working DB.. Just some tweaking to do now.

For those that haven’t seen the advert on www.suguk.org, There’s a hosted event at HP in Bracknell tommorrow evening, where among other things, HP’s SharePoint sizing model will be revealed. I for one am looking forward to playing with this and seeing if HP agrees with my design back at the office.

Tue 8 May 07

Long weekend..

Filed under: General — Reginald @ 8:42 am

Mmm.. lovely.. a nice long weekend.. And I didn’t touch code, SharePoint or Excel for the whole of it.. I did however play World of Warcraft for some of it!!!.. (Level 29 Human Warrior.. nice level to be at..)

I also had a good spring clean of my Garage, something about having a good clear out.. Mana for the soul and all that.. Just why do we keep every tin of paint we ever buy for the house? You’re never really going to go back in 2 years time and touch up a spot that’s been damaged.. It would never match, so you end up re-painting the entire room anyhow..

And I did some baking for a change.. Been a while.. made some rock buns to munch while I sort out the next sections of code…

Tue 1 May 07

The Excel properties predicament – A resolution..

Filed under: Excel, SUGUK — Reginald @ 4:35 pm

Well thanks to a fellow SUGUK member, I found the solution to passing Date values from Excel into a SharePoint document library.

‘The devil is in the detail’ as the saying goes and it’s no truer in this instance.. The trick is making sure that the date format of the text you’re passing out of Esxcel matches what SharePoint expects to see in a date format library column. Basically it must meet a particular format of ISO8601, which basically equates to yyyy-mm-ddThh:mm:ssZ

ISO8601 specifies that dates should be formatted year, month then day, and times should be formatted hours, minutes then seconds. If they are stored in a single string, then they should be seperated by a Capital T. If a timezone is not specified, then the ISO8601 assumes local time. In order to specify GMT, we add Z for ‘Zulu time’ or ‘Zero Meridien’.

So, how do we access this type of date? Well within VB or C# code, we can make a call to the Mirosoft.SharePoint.Utilities namespace and use the SPUtility.CreateISO8601DateTimeFromSystemDateTime method. Unfortunately, this isn’t as easy to achieve from VBA code within Excell 2003. But, we don’t have to..

All you need is a simple text format command, SharePoint will happily accept a properly formatted string and then treat it as a date..

=TEXT(cell_reference,”yyyy-mm-ddThh:mm:ssZ”)

Where cell_reference is a named cell containing a standard date/time.

Now, create a named cell reference for the new cell, Head to the properties page for the spreadsheet and create a linked property that points to the new cell. Make sure you have a library column that matches the named property, then upload the spreadsheet to your document library et voila, your date will appear..

Powered by WordPress