Manipulating SharePoint 2010 forms with JQuery

From time to time a user will want different views when editing a form I.e. display different fields to the user. The correct method to accomplish this would be the use of multiple content types to expose the relevant fields for each scenario. But at times this is not enough and the developer will need to write code to achieve the desired effect. Below is some useful JQuery for accessing and manipulating fields in a SharePoint 2010 form at runtime.

Change value of save button based on id containing string of text

$(‘input[name$=”SaveItem”]’).attr(‘value’, ‘Claim’);

Get selected value of DropDown based on text contained in ID of control

$(“select[id$=PartOfDropDownID] :selected”).text()

Hide row containing dropdown based on controls asp.net id containing ContentTypeChoice text

$(“select[id$=PartOfDropDownID]”).parent().parent().hide();

Hide row containing input field based on controls asp.net id containing ContentTypeChoice text

$(“select[id$=PartOfDropDownID]”).parent().parent().hide();

Get Value of input field based on title

$(‘input[title$=”Claimed By Email”]’).val()

Make input field read only based on title of field

$(‘input[title$=”Claimed By Email”]’).attr(‘readonly’, ‘readonly’);

Add css class to input field based on title of field

$(‘input[title$=”Claimed By Email”]’).addClass(“myCssClass”);

Get value of a span tag based on asp.net ID containing LoginLabel

$(“span[id$=LoginLabel]”).text()

Publishing site spell check showing errors when none exist

When checking for spelling mistakes on a new publishing page SharePoint 2010 informed me I had 100+ spelling errors. As I hadn’t entered any text yet I knew something was wrong. Further investigation led me to a SharePoint:UserField I had in my master page & layout pages. For some reason the spell check was including this in my results.

The solution to this was to put the SharePoint:UserField within a span tag with a CSS class of noSpellCheck.

Then I added the following JQuery to the master page which adds the excludeFromSpellCheck attribute to the text area that is rendered at runtime.

<span class=”noSpellCheck”><SharePoint:UserField FieldName=”Contact” runat=”server” /></span>

<script>
$(document).ready(function ()
{
$(".noSpellCheck input").addClass("ms-spellcheck-false");
$(".noSpellCheck textarea").attr("excludeFromSpellCheck", "true");
});
</script>

Cannot delete FAST Search Service Application

Recently I tried to delete a FAST Search service application using central admin (Manage Service Applications). When I hit delete I was presented with this error message:

An update conflict has occurred, and you must re-try this action

Next I tried power shell:

$ssa = Get-SPEnterpriseSearchServiceApplication -id “464b5960-b0c5-4e44-9af4-24cae0dc33b3”
$ssa.unprovision(1)

$ssa = Get-SPEnterpriseSearchServiceApplication -id “464b5960-b0c5-4e44-9af4-24cae0dc33b3”
$ssa.Delete();

NOTE: Get the search service ID from central admin by hovering over the application service link.

Again I was presented with a similar error:

PS C:\Users\mark>;;;;;;;;;; $ssa.unprovision(1)
Exception calling “Unprovision” with “1” argument(s): “An update conflict has occurred, and you must re-try this action. The object SearchServiceApplication Name=FAST Search Query was updated by domain\user in the OWSTIMER (2428) process, on machine SERVER_NAME. View the tracing log for more information about
the conflict.”
At line:1 char:17
+ $ssa.unprovision <;;;;;;;;;;<;;;;;;;;;;<;;;;;;;;;;<;;;;;;;;;; (1)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Next I gave stsadm a go:

stsadm.exe -o deleteconfigurationobject -id “464b5960-b0c5-4e44-9af4-24cae0dc33b3”

Still no luck:

An update conflict has occurred, and you must re-try this action. The object SearchServiceApplication Name=FAST Search Query was updated by domian\user, in
the OWSTIMER (2428) process, on machine SERVER_NAME. View the tracing log for more information about the conflict.

At this point I was stumped so turned to the Internet and found a possible solution on Jeff DeVerter’s blog. Here’s the solution:

  • Stop the Windows SharePoint Services Timer service (Found in Windows Services)
  • Navigate to the cache folder
    In Windows Server 2008, the configuration cache is in the following location:
    Drive:\ProgramData\Microsoft\SharePoint\Config
    In Windows Server 2003, the configuration cache is in the following location:
    Drive:\Documents and Settings\All Users\Application Data\Microsoft\SharePoint\Config
    Locate the folder that has the file “Cache.ini”
    (Note: The Application Data folder may be hidden. To view the hidden folder, change the folder options as required)
  • Back up the Cache.ini file.
  • Delete all the XML configuration files in the GUID folder. Do this so that you can verify that the GUID folder is replaced by new XML configuration files when the cache is rebuilt.
  • Note When you empty the configuration cache in the GUID folder, make sure that you do not delete the GUID folder and the Cache.ini file that is located in the GUID folder.
  • Double-click the Cache.ini file.
  • On the Edit menu, click Select All. On the Edit menu, click Delete. Type 1, and then click Save on the File menu. On the File menu, click Exit.
  • Start the Windows SharePoint Services Timer service
  • Note The file system cache is re-created after you perform this procedure. Make sure that you perform this procedure on all servers in the server farm.
  • Make sure that the Cache.ini file in the GUID folder now contains its previous value. For example, make sure that the value of the Cache.ini file is not 1.

Now I’m able to delete the FAST Search service application using central admin, power shell and stsadm successfully.

Bookmarking to different pages in SharePoint 2010 bug

When recently working on a clients SharePoint 2010 intranet site a publisher pointed out to me that when linking to a bookmark on different page, the page would load at the top and not where the bookmark existed.

Further investigation led me to the sp.ribbon.js javascript file. This caused the page to load at the top and not at the bookmark. My solution to this was to use the ExecuteOrDelayUntilScriptLoaded Javascript function to reload the page pointing to the bookmark once the sp.ribbon.js had fully loaded.

function GoToBookmark()
{
window.location.hash = self.document.location.hash.substring(1);
}
if (self.document.location.hash.substring(1) != "")
{
ExecuteOrDelayUntilScriptLoaded(GoToBookmark, "sp.ribbon.js");
}