Using SPFilterDropdown to Reorder Options in a Dropdown on a SharePoint Form

Many times when you have a dropdown column in your lists, you’d like to show the values sorted in a different order than alphabetically. Sure, you can use a Choice column and order the choices in any way you’d like. However, in the cases where you want the dopdown to be based on a Site Column which is a Lookup into a list, you can’t specify the sort order. I prefer using a Lookup column in almost all cases because it means that an admin (or any user with permission, really) can update the values in a list context rather than a column settings context. We also often end up storing other information about the values in the list, which we can’t do with a Choice column.

Here’s an example. I have a Site Column called Recommendation. It’s a pretty common idea: what’s our view on a particular stock or bond? We may want to recommend to our clients that the stock should be a Strong Buy, Buy, Hold, or Sell. If we add those values to a list as the source for a Lookup column, they will be ordered like this: [Buy, Hold, Sell, Strong Buy]. That’s not how we logically think of things, so it’s not great.

SPServices to the rescue! If we add a column to the source list which contains the sort order we want, we can make things look the way we need them to work. In my list below, I have a column called, logically enough, SortOrder. Note that it contains values which will sort the recommendation options the way we want.

Recommendations Datasheet View

Recommendations Datasheet View

Then, in the forms where I use the Recommendation column, I can add a call to the SPFilterDropdown function which takes care of things for me. No, the function name really doesn’t indicate that it could help with this – I built it to let you filter the options in a dropdown based on a column in the source list – but there’s a little trick you can use. If you specify a condition in your CAMLquery which always evaluates to true and specify a relationshipListSortColumn, the function will sort the values the way you want.

For my example above, I specify SortOrder as the relationshipListSortColumn, and set the CAMLQuery to give me all items where the Title does not equal an empty string. That will return all of the items, they will be sorted by SortOrder, and the dropdown will look just the way we want it to.

Recommendation Dropdown

Recommendation Dropdown

// If the Recommendation column exists, sort it as indicated in the source list
$().SPServices.SPFilterDropdown({
 relationshipWebURL: "/", // The source list for the Recommendation site Column is in the root site
 relationshipList: "Recommendations",
 relationshipListColumn: "Title",
 relationshipListSortColumn: "SortOrder",
 columnName: "Recommendation",
 CAMLQuery: "<Neq><FieldRef Name='Title' /><Value Type='Text'></Value></Neq>", // Get all values by specifying a non-blank Title
 debug: true // Debug mode on while we're developing
});

But Is jQuery Already Loaded?

JQuery
JQuery (Photo credit: Wikipedia)

Many end users are taking advantage of all of the goodness that jQuery provides, whether by adding simple effects like a sliding Quick Launch, more sophisticated form enhancements like SPCascadeDropdowns from SPServices, or even writing their own scripts for more complex solutions. More than ever, the distinction between “end user” and “developer” is blurring. IMO, that’s a good thing, but it also introduces complications, of course.

As one of those end users, you owe it to yourself to talk to your SharePoint team to explain what you are doing and to find out if jQuery is already loaded in the environment. In many cases, it will already be loaded in the master page or a page layout so that it is readily available across the Site Collection. The version that is loaded can also have an impact on how plugins function, making some of them effectively useless if there’s a version mismatch.

I know that in some cases, even finding the person who might know what’s loaded in the master page or what the plans may be is well nigh impossible. In those cases, here’s a trick to find out what’s up.

In a Internet Explorer browser window with the page you want to work with open, hit F12 to open the Developer Tools, and go to the Console. The screenshot below is from IE9, where there is a separate Console tab:

SNAGHTML562e339

If you’re in IE8, the Console is hidden under the Script tab, as below.

SNAGHTML5632086

If you’re in an older version of IE, well, I don’t have a screenshot for you. You may also not have the Developer Tools available, as it was a separate download with IE7 and below.

In Firefox, you’ll need the firebug add-in. If you have it, you can hit F12 to open Firebug. The Console tab is the first one on the left:

SNAGHTML56920f4

Once you’ve gotten to the Console, wherever it is, type the line I show in each of the screenshots above:

jQuery.fn.jquery

Depending on where you are, you’ll then either hit enter or click on the Run button.

Somewhere on the screen, you’ll see a display like this one, which shows that jQuery version 1.7.1 is loaded…

SNAGHTML56b32de

…or you’ll see something like…

SNAGHTML56d0846

…which tells you that jQuery isn’t loaded at all.

At that point, at least you’ll know what you are dealing with. If the version number isn’t what you need (I sometimes still see jQuery 1.3.2 loaded in a master page, which is ancient) then you’ll need to talk to your SharePoint team about updating it.

Tip ‘o the skimmer to Matt Bramer (@ionline247) for the exact syntax…

@ @ type in the console: jQuery.fn.jquery; ~ Give version num and tests whether or not it's loaded.
@iOnline247
ɹǝɯɐɹq ʍǝɥʇʇɐɯ【ツ】

…and @BinaryJam for asking the question in the first place!

something I'd love to see in getting started with jQuery articles ".Go view the source of the page, your devs may have already included it"
@BinaryJam
Binary Jam
Enhanced by Zemanta

The jQuery .css() Function versus CSS

Box model in CSS

Box model in CSS (Photo credit: Wikipedia)

A very smart designer who I respect a lot asked me a question today via IM that I thought would make a good post.

…so I know jquery is great for doing things with CSS – but I think of only using it when I can’t actually get to the tag or it needs to be changed on a particular event – but do you think it is better to make CSS changes in jquery vs CSS – what are the pros and cons here.

Here’s my thinking about this. I would always err on the side of CSS if possible. jQuery should be used to change CSS based upon some sort of user-generated event.

Many people seem to use jQuery to make CSS changes on page load (in a $(document).ready()) when they have trouble getting the CSS selectors right. If the selector is hard to get right in CSS, then it’ll probably be pretty hard in jQuery as well because the jQuery selector syntax is roughly the same as CSS3 selectors. jQuery gives you the ability to traverse the DOM, of course, so it can be easier to find an “anchor element” from which to navigate to the element you want to style.

My mantra is something like:

Ask the server to paint the page. Use jQuery to give it behaviors and interactivity.

Setting CSS with script on page load violates this in that it’s doing part of that initial painting. There’s also going to be at least a small delay until the script loads which can make the page load look “funny” (technical term).
You’ll see a lot of blog posts that show setting CSS on page load with jQuery, but think carefully in each case about whether it makes sense. If you’re doing it just to get around figuring out the right selectors in your CSS, then think again.

I’ll admit that sometimes I cheat on this, especially if I happen to have a .js file open instead of a CSS file. But I also try to make a pass back through my scripts to move things into the CSS that ought to have been there in the first place. You should too!

Enhanced by Zemanta

Configuring Lync Public IM Connectivity on Office365: But Can I Really Use It?

I use Office365 on an E1 plan to run my massive business of one. With that plan, I get Exchange, SharePoint, and Lync. It’s a fantastic deal for about $10 a month and works great for me.

I’ve sort of ignored the Lync component since there are so many other (somewhat better) options out there. But it seemed that I should at least get it set up so that I can do videoconferencing with my clients, primarily IM and screensharing. When I went to configure things, I ran into this gem of a help page (highlighting mine):

Public IM Connectivity Help

I feel like calling the documentation police on this one. I’ve read the two highlighted sections about 10 times each and they seem to directly contradict each other. Is it me?

If I can’t use Lync to do real work (IM, screensharing, audio, video) with anyone else who has Lync, then I’m not sure what the point of having it is. But from this documentation, I really don’t know!

One additinoal bote: I’ve got federation witrh external domains set up to work with all but blocked domains. In other words, I want this to be as open as possible:

SharePoint Designer Error When Saving a Page with Multiple Data View Web Parts

I could probably write an entire book on the strange things that SharePoint Designer does, but I don’t have time for that. Instead, it’s yet another blog post.

Today I was adding some Data View Web Parts (DWVPs) to a page, and I took the shortcut of copying and pasting the code of one to create another in the same page. This always works, but there are some things you have to do to pull it off.

When I went to save the page, I got this rather cryptic error:

SNAGHTMLf1de879

Server error: The URL Pages/Employee Handbook.aspx is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web.

Now, obviously the page existed, as it was the one I was editing, and the path was right, because it was the original location.

After thinking about it for a few minutes, I realized that I had forgotten to change the GUID in the copy of the original DVWP. I had two DVWPs in the same page with the same GUID, which is a no-no. Of course, the error is totally confusing and doesn’t indicate the actual problem.

If you copy and paste a DVWP’s code to create a new one, you have to change [at least] two attributes in either the original or the copy. Both the ID and the __WebPartId must be unique. They will look something like this:

ID=”g_ff043ef3_b944_4912_b32d_6ff419e69761″

__WebPartId=”{476FBB3B-91DD-4FDA-A20C-7FB1DA457B09}”

I admit that I always cheat on this and simply change the last character of the two GUIDs. I know I’m supposed to use some GUID generator, but the only place that these two GUID must be unique is within the current page.