Archive

Posts Tagged ‘JQuery’

Using jQuery to return all members / users of a group #in #SP2010 #SharePoint

January 12, 2011 1 comment

I’ve recently had cause to determine if a user is within a particular group in SharePoint and act upon the output.

Luckily the standard SharePoint web services allow to query for this information utilising the User Group service at location: http://<server>/_vti_bin/usergroup.asmx.

In particular the method GetUserCollectionFromGroup will provide the complete list including the Windows SAMAccountName (login name) as part of the return.

However, in this instance I was caught a little by a permissions problem.  Although I could query the web service, it kept returning a status of “parsererror” and an output of “Access Denied”.

This was resolved by changing the group settings to allow “Everyone” to read the membership of the group.

GroupSettingsBlog

Code Example

The following code utilises jQuery to access the web service:

1 <script type='text/javascript'> 2 3 _spBodyOnLoadFunctionNames.push("getUserCollectionFromGroup"); 4 5 function getUserCollectionFromGroup() { 6 var soapEnv = 7 "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \ 8 <soap:Body> \ 9 <GetUserCollectionFromGroup xmlns='http://schemas.microsoft.com/sharepoint/soap/directory/'> \ 10 <groupName>Group Name Here</groupName> \ 11 </GetUserCollectionFromGroup> \ 12 </soap:Body> \ 13 </soap:Envelope>"; 14 $.ajax({ 15 url: "/_vti_bin/usergroup.asmx", 16 type: "POST", 17 dataType: "xml", 18 data: soapEnv, 19 complete: getUserCollectionFromGroupReturn, 20 contentType: "text/xml; charset=\"utf-8\"" 21 }); 22 } 23 function getUserCollectionFromGroupReturn(xData, status) { 24 alert("Status: " + status); 25 alert("Output: " + xData.responseText); 26 } 27 </script> 28

Determining delegation mode and user via jQuery #in #ProjectServer #PS2010 #SP2010

January 12, 2011 Leave a comment

In my current project I have recently had the requirement to determine whether Project Server 2010 was in delegation mode.

If the user was in delegation mode, we then passed the unique Display Name of the delegation user and passed it to a web service to return more data about the resource.

DelegateScreenshot

The information bar in PS2010 lets us know that we are in delegation mode and in jQuery we can interrogate this to get the information we need.

The following code uses jQuery to determine if we are in delegation mode and sets the Display Name and isDelegate variables accordingly:

1 <script type='text/javascript'> 2 var delegateName = ""; 3 var isDelegate = 0; 4 5 _spBodyOnLoadFunctionNames.push("checkDelegateMode"); 6 7 function checkDelegateMode() { 8 //Am I in delegate mode? Check the SP2010 Information bar for the word delegate 9 $('span[id=status_1_body]').each(function() { 10 11 var textEval = $(this).text(); 12 13 if (textEval.indexOf('delegate') != -1) { 14 //The word delegate has been found - lets find the delegate Resource Display Name 15 16 var strongCount = 0; //The delegate is the first item surrounded in the <STRONG> tag 17 18 $(this).find("strong").each(function() { 19 20 if (strongCount == 0) { 21 delegateName = $(this).text(); //Store the display name 22 isDelegate = 1; //Set that the delegate has been found 23 24 } 25 26 strongCount = strongCount + 1; 27 28 }); 29 } 30 31 }); 32 } 33 </script>

Using jQuery to find the current logged in user #in #SP2010

January 11, 2011 2 comments

Problem:

In MOSS 2007, we had methods to get the current logged in user by accessing some of the properties found in the Welcome Menu.

Although this is still the case in SharePoint Server 2010, there are some fundamental changes to the code and the way the Welcome Menu is rendered in SPS 2010 that means we have to do something slightly different to get the same result.

I have recently had a requirement to achieve this in my current project and thought I would share the basics with you.

Essentially we are still retrieving the information from an HTML element property in the Welcome Menu, but the menu itself is rendered in AJAX and therefore is not necessarily available as soon as the page is rendered and the DOM document ready state is set.

As a result of this, running JavaScript / jQuery on the page at either:

  • $(document).ready
  • _spBodyOnLoadFunctionNames

will not return the required result, since the menu elements we need to interrogate have not been rendered yet.

Solution:

  • Set a timer to keep running a particular function to wait until the menu is rendered
  • Stop the timer
  • Interrogate the DOM as required using jQuery

Code Example:

1 <script type='text/javascript'> 2 3 var timerHandle = ""; 4 var currentWindowsAccount = ""; 5 _spBodyOnLoadFunctionNames.push("runCustom"); 6 7 function runCustom() { 8 timerHandle = setTimeout(getCurrentLoggedInUser, 10); 9 } 10 11 function getCurrentLoggedInUser() { 12 //Function requires jQuery 13 14 /* Retrieve the current logged in user from the My Profile option in ** 15 ** the Welcome Menu (id=ID_MySiteLinksMenu) ** 16 ** This needs a timeout to wait for the menu to be rendered via AJAX */ 17 18 //Timers are expensive on client resources, clear at all opportunities 19 clearTimeout(timerHandle); 20 21 var welcomeMenuItems = $('#ID_MySiteLinksMenu'); //ie:menu with id='ID_MySiteLinksMenu' 22 23 if (welcomeMenuItems.length > 0) { 24 var onmenuclickValue = welcomeMenuItems.attr("onmenuclick"); 25 var onmenuclicksplit = onmenuclickValue.split("="); 26 var loggedInUserString = onmenuclicksplit[1]; 27 var loggedInUserStringSplit = loggedInUserString.split("'"); 28 29 currentWindowsAccount = loggedInUserStringSplit[0]; 30 alert(currentWindowsAccount); 31 } 32 else { 33 //My Profile link has not been rendered yet, try again. 34 timerHandle = setTimeout(getCurrentLoggedInUser, 10); 35 } 36 } 37 38 </script>

Categories: Work Tags: , ,

Site Asset List View and jQuery issue and resolution #in

November 26, 2010 3 comments

So recently I have been working on a project where we make use of the new Site Asset library capabilities in SharePoint 2010.

The Site Asset library comes with new functionality including pop-up previews of images and videos.

In particular, we are taking advantage of the fact that by uploading an image into the site library, 2 thumbnail versions are created for the list view preview and the pop-up preview.

This provides some good unique user experiences previously not seen in SharePoint out of the box before.

Anyway, to the jQuery issue:

It would appear that the Site Asset library (standard list view) uses the $ call for its JavaScript functions.  This obviously conflicts with the standard way to call jQuery functionality:

1 $.(this)

 

This results in the error:

  • Object doesn’t support this property or method

image

Resolution:

jQuery being implemented onto the master page doesn’t pose a problem since the SharePoint code is obviously called afterwards.

So by referencing jQuery in its longhand method:

1 jQuery.(this)

 

you can use both the site asset functionality as well as any jQuery customisations that you may need to do.

Hopefully that helps out fellow SharePoint customisers out there.

Categories: Work Tags: ,

#SP2010 Group List Items by External Data field–Field Display Missing #in

November 17, 2010 2 comments

On an intranet I have been working on recently I came across a bug where the display of an External Data field in the 1st and 2nd level grouping is not shown when grouping by the BCS based item(s).

To provide a short term fix I created the following JavaScript to run on the page to solve the problem.

1 <script type="text/javascript"> 2 3 _spBodyOnLoadFunctionNames.push("updateGroupNameValues_Level1"); 4 5 function updateGroupNameValues_Level1() 6 { 7 $('TBODY[id*=titl]').each(function() 8 { 9 var currentGroupString = unescapeProperly(($(this).attr("groupstring"))); 10 currentGroupString = currentGroupString.replace(';', ''); 11 var groupStringArray = currentGroupString.split("#"); 12 var groupLevel1 = groupStringArray[1]; 13 var groupLevel2 = groupStringArray[2]; 14 15 var currentSpan = $(this).find("span").text(); 16 var newSpan; 17 if (groupLevel2 == "") 18 { 19 newSpan = groupLevel1 + " " + currentSpan; 20 $(this).find("span").text(newSpan); 21 22 } 23 else 24 { 25 newSpan = groupLevel2 + " " + currentSpan; 26 $(this).find("span").text(newSpan); 27 } 28 29 //alert("groupLevel1: " + groupLevel1 + " | groupLevel2: " + groupLevel2); 30 //alert(($(this).children("span").html())); 31 }); 32 } 33 34 </script> 35

Essentially the values do exist in the DOM / HTML, but they are not displayed in the tags presented by the grouping.

NB: Please be aware that this solution relies upon JQuery.

Useful JavaScript Function: Hide Side Panel / Quick Launch – v4.master

October 11, 2010 Leave a comment

On dashboard pages I often find I need more space.  A quick way to do this is to place this into a Content Editor Web Part.

1 <script type='text/javascript'> 2 3 function v4MasterHideSidePanel() { 4 $('#s4-mainarea').children().eq(0).hide(); 5 $('#s4-mainarea').children().eq(1).css('margin-left', '0'); 6 } 7 8 $(document).ready(function(){ 9 v4MasterHideSidePanel(); 10 }); 11 12 </script>

This will hide the Side Panel / Quick Launch area on the v4.master – Master Page (Default Master Page for non-publishing sites)

NB: This method relies on JQuery

Categories: Work Tags: ,
%d bloggers like this: