November 30, 2011

SharePoint: How to clear Metadata and People Editor fields using jQuery

Challenge:

NewForm and EditForm of a custom SharePoint list contain Managed Metadata field and People Editor fields. I need to be able to clear the field values dynamically based on what selections the user makes when filling the form.

Just hiding the fields is not good as I don't want to include an additional column only for storing the value of the radiobutton selection.

image

image

Solution:

This is possible by using few Javascript tricks, and as I'm most familiar with jQuery I decided to use it to accomplish the goal. There are few hidden fields you need to modify in addition to the visible elements.

First you need to surround the fields with DIV's so that it is easier to reference the correct elements.

<asp:RadioButton runat="server" id="presenterSisainen" Text="Sisäinen" GroupName="presenter" />
<div id="presenterUserDiv" style="display:none;">
<SharePoint:FormField ... />
<SharePoint:FieldDescription ... />
</div>
<asp:RadioButton runat="server" id="presenterAsiakas" Text="Asiakas" GroupName="presenter" />
<div id="presenterMetadataDiv" style="display:none;">
<SharePoint:FormField ... />
<SharePoint:FieldDescription ... />
</div>

To clear Metadata field, do this:

jQuery("#presenterMetadataDiv > span > span > input[type='hidden']").val("");
jQuery("div[title='DISPLAYNAMEOFMETADATAFIELD'] > div > span[class$='valid-text']").replaceWith("&#8203;");
jQuery("#presenterMetadataDiv > span span.ms-formvalidation[role='alert']").html("");
jQuery("#presenterMetadataDiv > span > br").remove();

Note: 'DISPLAYNAMEOFMETADATAFIELD' needs to be the display name of the metadata field in question. It can contain spaces, etc. If it contains special characters such as umlauts, remember to save your .js file using UTF-8.

To clear People Editor field, do this:

j("#presenterUserDiv input[id$='HiddenUserFieldValue']").val("");
j("#presenterUserDiv span[id$='UserField']").attr("editoroldvalue", "");
j("#presenterUserDiv input[id$='UserField_hiddenSpanData']").val("");
j("#presenterUserDiv div[name='upLevelDiv']").html("");
j("#presenterUserDiv span[id$='UserField_errorLabel']").html("");

Note: You can use different jQuery selectors to achieve the same result, so above is not the only truth.

November 24, 2011

SharePoint: Some files not visible after migrating Content DB between Classic/Claims enabled Web Applications

Problem:

After migrating a SharePoint 2010 Content DB to a freshly installed environment my style sheet located in /Style Library suddenly disappeared.

Well, without really thinking it through I created new .css file and renamed it to what it was and should be. I got error saying such file already exists. Say what?

image

Reason:

What I didn't immediately remember was that when Content DB backup was taken, it was from a Web Application that was using Classic Mode Authentication.

image

The new environment instead is using Claims Based Authentication. So, what was going on was that my current user account didn't see the style sheet because the file was checked out to classic-based-me instead of claims-based-me at the time when backup was taken.

In addition that file had never been checked in by classic-based-me so there was no way I could see it when logged in with claims-based-me (from SharePoint permissions perspective they are two different user identities).

Solution:

  1. Navigate to the list in question and go to settings of that list.
  2. On List Information screen you see "Manage files which have no checked in version"

    image
  3. Select the file(s) you need to gain access to and select Take Ownership of Selection

  4. image
  5. BOOM! You have regained access to the file(s)!

November 21, 2011

Is your external anonymous SharePoint site a bit too open?

Problem:

Not exactly my problem, but a friendly reminder that it but might be your problem if your site is found here:

http://www.bing.com/search?q="Compatible+UI+Version(s)"+"checked out to"+site:<YOURSITEURLHERE>

Solution:

Activate ViewFormPagesLockdown feature as instructed here in MSDN.

If you’re using Publishing Portal site template, this feature is already activated, but for other templates you need to enable it manually.

The MSDN article contains also other suggestions for public facing anonymous SharePoint sites that you might want to follow.

November 18, 2011

SharePoint: CAML filtering for Managed Metadata field that is empty

Problem:

How to write CAML filter that would show items where a specific Managed Metadata field is empty?

Solution:

You need to use IsNull.

<Query><Where><IsNull><FieldRef LookupId="TRUE" Name="ManagedMetadataColumnName" /></IsNull></Where></Query>

LookupId=”TRUE” is optional in this case. If you include it, you are actually comparing if ID of the metadata field value is NULL. If you leave it out, you are comparing if the actual text of the metadata field value is NULL. Either way, same result.

SharePoint: How to get lookup ID of a Managed Metadata Term without having to use GetWssIdOfTerm()

Problem:

In SharePoint Online (Office 365) and on-premise SharePoint, how do I get the lookup ID of a Managed Metadata Term without having to write code anad use GetWssIdOfTerm() method?

Solution:

Go to the hidden list at the root of your site collection, e.g., http://yoursite.com/lists/TaxonomyHiddenList and look at the ID field of the respective metadata item.

image

Further explanation on how to actually use that ID in a CAML query can be found here. It actually works in DFWP.

November 17, 2011

SharePoint: “Security validation for this page is invalid” when adding subsite programmatically

Problem:

I created a Web Part that is used for creating subsites. In Web Part properties you could type in the name of the Web Template you had uploaded to solution gallery, in addition to other properties. Web Part uses RunWithElevatedPrivileges.

For the first Web Template that was used everything worked fine and subsites could be created just fine. However, the slightly modifying the Web Template lead to the dreaded security validation error:

Exception attempting to ApplyWebTemplate to SPWeb https://URLOFTHENEWWEB: Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. ---> System.Runtime.InteropServices.COMException (0x8102006D)

Thoughts:

There wasn’t really much difference between the Web Template that was working and the one that couldn’t be used in my web part when creating subsites.

In fact, what was done in this slightly modified version of the Web Templates was to add an instance of a Document Library template that contained few custom Content Types.

What makes it interesting was that one could create new subsite using that template, if one would use normal SharePoint browser functionality for subsite creation.

As it happens every now and then, I couldn’t reproduce this issue on my development virtual server – even when using the same Web Template.

worksonmymachine

Solution:

Although it is not exactly clear why having Document Library template on a web template would break subsite creation, there is a real solution.

What had forgotten was to call SPUtility.ValidateFormDigest() before attempting to add the subsite.

Also MSDN instructs that “Either SPUtility.ValidateFormDigest or SPWeb.ValidateFormDigest should be called before a call of RunWithElevatedPrivileges, if the method passed to the RunWithElevatedPrivileges includes any write operations.”

Reason for running the validation is to prevent cross-site-scripting exploits.

   1:  SPUtility.ValidateFormDigest(); // <<<<< REMEMBER THIS!
   2:  // ...
   3:  // Create site
   4:  using (SPWeb newWeb = parentWeb.Webs.Add(
   5:        projectUrl,
   6:        projectName,
   7:        "",
   8:        Convert.ToUInt32(locale),
   9:        siteTemplate,
  10:        true, // True breaks inheritance
  11:        false))
 



Thanks to Arttu Arstila for helping me with this.

November 15, 2011

SP2010: Things to check if your Data Form Web Part is misbehaving

Challenge:

You’re working with SharePoint Designer 2010, and trying to add Data Form Web Part to your Page Layout. There are numerous articles explaining how you should change ListID to ListName and everything should be fine.

Well, the DFWP road might sometimes be long, so here are few things I noticed that might save you a few moments especially when trying to make reusable DFWP that would reference to a list always on the site on which the Web Part is sitting in.

Solution:

  1. You must remove attributes related to WebURL if you want to make reusable DFWP
    1. Remove WebURL from WebPartPages:DataFormWebPart > ParameterBinding
      and
      SharePointWebControls:SPDataSource > (Select/Delete/Insert/Modify)Parameters > WebPartPages:DataFormParameter
  2. You can remove SharePointWebControls:SPDataSource > (Delete/Insert/Modify)Parameters if you only use Web Part for showing items
  3. For reusable DFWP you must remove WebPartPages:DataFormWebPart’s attributes called listname and ListID.
  4. For reusable DFWP you must replace ParameterBindings and xsl:params ListID with ListName
    1. Also remember to change the GUID to list name
  5. If your list name contains space character, it’s OK, you can have space character in ListName attribute
  6. NOTE! SharePoint Designer keeps adding some attributes to DFWP even if you don’t use Save. It’s enough if you just close the file and reopen it, the ListName and ListID attributes with GUID values can suddenly be in WebPartPages:DataFormWebPart node.
  7. If it still doesn’t work, remove id and __WebPartId attributes form the WebPartPages:DataFormWebPart tag and let SPD generate them again.
  8. Always keep notepad open and copy paste your working code there
    1. Before copying the working markup to notepad, make sure SPD hasn’t injected ListName and ListID GUID attributes from WebPartPages:DataFormWebPart node
  9. Do not do this:
    1. Edit Page Layout file in SPD window
    2. Save file
    3. Close the Page Layout file window in SPD <<<---- No, no, no!
    4. Check in file in SPD
      --> SPD will insert ListName and ListID attributes into your DFWPs
  10. Do this instead:
    1. Edit Page Layout in SPD window
    2. Save file
    3. In another SPD window in folder view check in your Page Layout file
    4. Close the Page Layout file window

Finally, if you're getting the "List does not exist" error and you're code is exactly how it should be, copy the last working version of DFWP from your backup file. If you compare the working and non-working DFWP's you won't probably notice any differences; at least I didn't; so it might be enough just to cut&paste the same DFWP code back.

Technorati Tags:

November 10, 2011

Visual Studio 2010: Comparing files without TFS, yes it is possible!

Question:
Do I always need connection to Team Foundation Server or download some 3rd party tool in order to compare files or folders when I have Visual Studio 20XX installed?
Answer:
No. There is indeed file and folder comparison utility bundled to Visual Studio, but it is well hidden.
You can find the WinDiff.exe from C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64. Just make shortcut of it to Start menu, or add it to Tools menu in Visual Studio.

image

November 8, 2011

SP2010: This upgrade session has been stopped

Problem:

After installing SP2010 SP1 and trying to run psconfigui (UI version of the Configuration Wizard) you end up with error:

[psconfigui] [SPUpgradeSession] [INFO] [11/8/2011 3:29:14 PM]: No context object
[psconfigui] [SPUpgradeSession] [ERROR] [11/8/2011 3:29:14 PM]: This upgrade session has been stopped. Possible causes include the process being terminated abruptly or the OS has rebooted. Please restart the upgrade again.

The only sensible error you see in PSCDiagnostics log is:

The exclusive inplace upgrader timer job failed.

Followed by generic error:

Failed to upgrade SharePoint Products.

You retry the Configuration Wizard, and you end up with same error.

Solution:

Run psconfig.exe (command line version) from command line and don’t use the graphical UI version.

Command to use: psconfig -cmd upgrade -inplace b2b -wait

November 4, 2011

WHS 2011: Remote connection to home network computers not working

Problem:
When trying to connect to computers in your home network through Windows Home Server 2011's Remote Desktop Gateway feature you get error message:

Your computer cant connect to the remote computer because no certificate was configured to use at the Remote Desktop Gateway server. Contact your network administrator for assistance

Thoughts:
You have generated custom certificate for your domain (not self-signed), or you may have bought a real certificate but anyway you have configured to use that certificate in IIS instead of letting WHS take charge of things.

Solution:
You need to assign existing SSL certificate to the Remote Desktop Gateway manually in the RDG console. That console is not installed by default in WHS 2011, so you need to first install that role, then pick a certificate.

Not to repeat what has already been said, configure your browser to Paul Sterley's great article on how to get this done.