PART 1 : Working in MOSS using SPServices

After working in SharePoint 2010 and 2013 platform, it was bit difficult for us to work in SharePoint 2007.  SPServices came to our rescue. I have consolidated different scenarios we encountered and placed them in a single post below. So, it might help someone in the future.

Download the below files, save and provide a reference to these scripts before using the below script:

SPServices to populate people picker with logged-in username and to disable it in IE and Chrome:

<script type="text/javascript"> 
    var currentUserName ;   
    $(document).ready(function() {
    currentUserName = $().SPServices.SPGetCurrentUser({
    fieldName: "Title",
    debug: false
});
 alert(currentUserName);
 if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )) //IF IE > 10
      {
          alert("Browser used for display is IE");          
      $("div[title='People Picker']").text(currentUserName);
      $("div[title='People Picker']").prop('disabled',true);
       $('img[title="Browse"]').css('display','none');
       $('img[title="Check Names"]').css('display','none');
        
      }     
      else if(navigator.userAgent.indexOf("Chrome") != -1 )
      {
          alert("Browser used for display is Chrome");  
   $("textarea[title='People Picker']").val(currentUserName);
      $("textarea[title='People Picker']").prop('disabled',true);
      $('img[title="Browse"]').css('display','none');
      $('img[title="Check Names"]').css('display','none');    
        
      }
 });
</script>

SPServices to check whether the user belongs to the specific SharePoint group(say, Admin):

<script language="javascript" type="text/javascript">
$(document).ready(function() 
{
    $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='Admin']").length == 1)
        {
      alert("You belong to admin group");
  //your code for admin users
    }
        else
        {
          alert("You do not belong to admin group");
  //your code for non-admin users
        }
      }
   }); 
}); 
</script>

SPServices to perform read operation:

<script>
$(function(){
getListItems();
});
function getListItems(){
var method = "GetListItems";
var list = "Demo";
var fieldsToRead =  '<ViewFields></ViewFields>';
var query = "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>"+ID+"</Value></Eq></Where></Query>";            $().SPServices({
operation: method,
async: false,
listName: list,
CAMLViewFields: fieldsToRead,
CAMLQuery: query,
completefunc: function (xData, status) {
$(xData.responseXML).SPFilterNode("z:row").each(function () {
var eTitle = ($(this).attr("ows_Title"));
alert(eTitle);
});
}
});
}
</script>

To get the latest version of SPServices and to know more about it, visit this link

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s