Monday, April 12, 2010

Lookup Fields and XSL

Ahh, XSL...Cause of and solution to all of SharePoint's formatting woes.

I'm displaying a document library in an XSLT data view on my SharePoint page, and one of the fields I want to show is a lookup type.

Now, lookup type field values are stored with both the string value and the ID, like so:

17;#Value


...Which is fine, right up until you try to include that field in your data view, and get pages of 17#;Value, 18#;AnotherValue, etc.

Thankfully, XSL provides its own solution. Use the substring-after command to strip out those pesky IDs....Or, conversely, use substring-before if you just want the IDs!


Code sample:


<xsl:value-of select="substring-after(@Fieldname, ';#')" />

Wednesday, April 7, 2010

Sharepoint Navigation: It Hurts So Good

Just when you think you've mastered navigation in SharePoint, something new gets tossed at you.

My own ego was deflated when a client requested a 3rd-level flyout on our top navigation, something like this:


HOME SUBSITEA SUBSITEB SUBSITEC
Nav 1
Nav 2
Heading->
Subnav 1
Subnav 2
Subnav 3


Sure, I confidently asserted, I can definitely do that! No sweat.

Smugly, I set up a heading and a bunch of links beneath that on SubsiteA. Still brimming with confidence, I clicked 'OK', and moused over the navigation bar to admire my work.

But...it wasn't there. Just...nothing. Did I save it properly? I doublechecked, and yes, I saw the heading and all the subnav items still there in the admin page. Mocking me. I could almost hear their laughter.

I mucked around with different combinations of relative and absolute URLs, and replicated the issue on other sub-sites. Sure enough, headings just refused to show up on anything except the root site.

Annoyed, I turned to the web for help, and finally dug up a post by Millerw that addressed the problem. Millerw suggested changing the data source for the navigation to "CurrentNavSiteMapProvider". I checked, and found that my source was "CombinedNavSiteMapProvider". Well, OK - worth a try!

I swapped sources, and checked out my site. Um...Not quite what I was looking for. Yes, I could now see the flyout on that subsite, but navigation to all other subsites was messed up. Everything was oriented vertically, I wasn't seeing all the subsites I wanted to, etc., etc.

Beginning to despair, I searched around and ended up on the Microsoft Enterprise Content Management Team blog (there's a mouthful), where they have a VERY good deep-dive on MOSS navigation. They actually had a lot of useful information on CombinedNavSiteMapProvider...And in their sample code, they had the following line:

TrimNonCurrentTypes="Heading"

AH-HAH, they're turning off 'heading' in the navigation - that MUST mean that CombinedNavSiteMapProvider can show headings!

Sure enough, by tweaking settings I was able to get the code below to work. Now I see all my horizontal top nav items just as before, with the addition of headers on my sub-sites.

I'll tell ya, if SharePoint is good for anything, it's good for keeping your ego in check.

The code:



<publishingnavigation><?xml:namespace prefix = publishingnavigation /><publishingnavigation:portalsitemapdatasource id="GlobalNavDataSource" treatstartingnodeascurrent="true" startingnodeoffset="0" startfromcurrentnode="true" showstartingnode="false" sitemapprovider="CombinedNavSiteMapProvider" runat="server"></publishingnavigation:portalsitemapdatasource>

Wednesday, March 3, 2010

With a Little Javascript From My Friends

Had an interesting problem with an XSLT data view today.

THE BACKGROUND: I needed to display a document library from another sub-site. I set up an XSLT data view to pull data from that document library. One of the fields I brought over was a multi-select lookup list. The values all came back in a semicolon-delimited format:

This;Is;What;The;Values;Looked;Like

Problem was, a number of rows had a very long list of tags in this column, and it wouldn't wrap on semi-colons...Although it WOULD wrap on hyphens. So sometimes you'd get:

A;Long;List;Of;Hypen-
Delimited-
Entries;That;Linebreak;Like;This

Massively ugly. I tried restricting the width of the column to try and force the data view to add line breaks, but that didn't work - the cells stayed very wide and wouldn't wrap where I wanted them to.

It took a bit of beating my head to my desk before I finally settled on the simplest solution. Convince CSS to wrap on a particular character? Count the number of chars, look for the next semicolon, and insert a page break after that? Rawrgh, too complicated!

Finally, I realized this could be tackled very simply. Word wrapping WAS working - just not on semicolons. What if we replaced them with a semicolon AND a space? This would keep things completely dynamic in case I wanted to change the column width in the future, too. But...how to make that happen?

In the end, I found two solutions. Some smart friends who know their way around Javascript helped put together the following:

function SeparateSemicolons( inStr )
{
var strArray = inStr.split(";");
return strArray.join("; ");
}

var clevervarname = document.getElementsByTagName("td");
var i = 0;

for (i=0; i < clevervarname.length; i++)
{
var place = clevervarname[i];

if( place.id == "TheTDWithSemicolons" )
{
place.innerHTML = SeparateSemicolons(place.innerHTML);
}
}


Placed at the end of the page in the appropriate tag, this did take care of the problem.

In conjunction, I poked around for a solution on the XSL side. I finally found a suggestion from David Carlisle here on replacing characters in XSL. I pasted in his code, changed the from and to parameters, and pointed it at my data field. Voila!

I think it's neater to do this on the XSL side, but it was great to get another round of education on Javascript at the same time.

Back to the grind!

Thursday, January 28, 2010

Sharepoint Page Editing Quick Fix

Looking to customize a system page (the edit item page for a document library), I found a hint online to quickly set your page to edit mode within the browser. Add the following to the end of your page's URL:

?PageView=Shared&ToolPaneView=2

This will put even system pages into edit mode. Saves the trouble of having to open Designer for a quick fix!

Tuesday, January 26, 2010

Oooh, you wascally job timer, you!

The SSP Timer Job Distribution List Import Job was not run.

Reason: Logon failure: unknown user name or bad password


...Every.
...Five.
...Minutes.

It's hard to think of a more often-discussed SP error than the SSP Timer Job failure message. This error shows up in the server's event log every five minutes, oftentimes in pairs. If you take a look at the security log, you should see an associated 529 error (or two) with the reason: Unknown user name or bad password, and the user name of the account used for your SP site's shared services provider.

It's an easy password change to miss, and it may be that you've just not gotten the new password into all the right places the last time it was changed. A number of forum posts advise that you run the following from the STSADM command line (at %COMMONPROGRAMFILES%\microsoft shared\web server extensions\12\bin):

stsadm -o updatefarmcredentials [-userlogin] [-password]

stsadm -o updateaccountpassword -userlogin -password


...Followed by an IISRESET. In a lot of cases, this will catch any password changes you didn't get to manually through Central Admin, and solve the problem.

I found that it wasn't clearing the problem on my server, however. Poking around some more, I found a post that suggested an additional step: in Central Admin, check to make sure you've added your Shared Services account to the "Process Accounts with access to this SSP" box in the configuration menu.

OHHHHHHH.

Sure enough, that took care of it! You just have to:

*Open Central Administration
*Click on the 'Shared Services Administration' link
*Click the drop-down menu for the name of your SSP, and select 'edit properties'
*Enter the new password in the 'SSP Service Credentials' section, for good measure
*Enter the SSP username in the 'Process Accounts with access to this SSP' section
*Click OK
*Perform an IISRESET

That did the trick!

Monday, December 14, 2009

Take THAT, Sharepoint Document Library Copy Procedure!

Copying documents from one location in SP to another: Should be easy, no?

Well, it seems that it depends entirely on how far you're moving them, and how much you care about metadata (created by, modified by, version history, etc.)

If you don't mind losing your metadata, just open your source and destination libraries in explorer view, and copy/paste or drag/drop as needed. This works for moves between doc libraries in the same AND in different site collections.

If you want to preserve metadata and you're just moving stuff within a single site, use the Site Content and Structure page to select and move your files.

What if you need metadata AND want to move items between two entirely different site collections, tho?

The best solution is probably a coded one - write some custom web service or some such to move all of that. A few 3rd party tools are also out there which can help with this. Unfortunately, I don't have the C# skills or the purchasing power to pursue either of those. What's a BI monkey to do?

Poking around for an alternative solution, it occured to me - say, don't backups preserve metadata? ...I don't want to overwrite my destination site for these files, but what if I created a temporary sub-site, dumped the backup there, and then used the Site Content and Structure page to move the files to their final resting place?

Worth a try! I hit the source site and backed up the first sub-site I was concerned about, using the stsadm Export tool. I created an empty sub-site on my destination site, plunked the resulting .cab file into my destination site's server, and ran the import procedure to dump the data into it.

It worked! It takes a little longer than a straight copy-paste, but it does preserve metadata on your files while moving them to the new location.

Tips:
If you have to use -nofilecompression to export your list, be sure to add the same parameter to your import procedure (syntax example here)

Make sure your source subsite and destination subsite are the same. I got an error saying mine weren't, and had to refer to this list to translate the template code it gave me.

When first running my -import, I got an error regarding the 'AllowAutomaticASPXPageIndexing' attribute. Found a hotfix and a workaround here, and used the workaround.

Doublecheck to make sure you use the –includeusersecurity parameter when exporting/importing; when I left that out, the ownership metadata dissapeared.

Thursday, December 3, 2009

Sharepoint: Item-Level Permissions in Doc Library Based on List Data

Had an interesting challenge to set item-level permissions on a SharePoint document library based on list metadata. If a user uploaded a document and set the value of a list column to "published", everyone could see it. It the document was "unpublished", only a restricted group could view the file.

After a lot of surfing around the web, I found Richard Seroter's excellent post, which covered the vast majority of what I needed.

As Richard did, I created both my main document library and a lookup list which contained my published/unpublished values, and a list of users associated with each.

NOTE: I tried using a people/groups field type for this column at first, but when queried, that returned both the field ID and value, like "1;DOMAIN/username;2;DOMAIN/user2". I finally switched to a multi-line text field instead, which was easier to query cleanly.

In my main document library, I set up a lookup field that pulls the title values from my lookup list. So far, so good!

In order to run the Sharepoint Server Sequential Workflow project in VS 2005 Richard used, I needed to nab first the Visual Studio 2005 extensions for .NET Framework 3.0 add-in, and then the SharePoint Server 2007 SDK: Software Development Kit.

NOTE: I actually ran into an issue where neither set of templates were showing up in my already-installed copy of VS 2005, so I ran a repair install before I could finally access them. Hopefully, it will just work for you. =)
OK, ready to run! I fired up VS, started the project, and got through the first couple of steps. I referenced another article at The Code Project for some of the finer details, including how to deploy the DLL to the GAC.

NOTE: Don't be a moron like I was. Make sure that you nab the PublicKeyToken when deploying the DLL, and fill that in wherever the code asks for it. Leaving it as '111111111111' doesn't work. ;)

When all was said and done, the project was finally deployed and it looked like things were working...but wait, that workflow that just said 'completed' is now 'in progress' again. We're getting 'out of memory' errors. What's going on?

Thanks to an article on how to debug your SP workflow, we found the problem: An infinite loop. No, not caused by our code - by the workflow itself.

See, the workflow was set to run when a new item is created OR when an item is changed. It turns out that when the workflow runs, it makes a change to the item...Which then triggers the workflow...Which makes a change...Which triggers the workflow...And you're stuck in an infinite loop. Brilliant.

Back to the web! An article on the same problem in another workflow scenario suggested simply disabling event firing immediately before the update is called, and turning it back on right afterwards.

That's the solution we put in for now, although I think it would be better long-term to move the IF statement up a notch. It could check and see whether the values it would be updating the list item to is already there. If old values = new values, do not update.

It's working for now, tho. Phew!