Wednesday, February 29, 2012

Article 5: Get group name in sharepoint using SPServices


Step 1: Open your site | Click Site Action | Site Setting | People and groups | Add new user is administrator
Step 2: Upload 2 file Javascript to Shared Documents (jquery-1.4.2.js, jquery.SPServices-0.5.6.js), you can download 2 file .js at here
Step 3: Click Site Action | Edit Page
Step 4: Click Add a Web Part
Step 5: Choose categories | Media and Content | Content Editor
Step 6: Click to Content Editor (Click here to add new content)
Step 7: Click HTML | Edit HTML Source
Step 8: Paste code to HTML Source and click OK
<script type="text/javascript" src="http://quochung-acer/mstechnology.vn/Shared Documents/jquery-1.4.2.js"></script>
<script type="text/javascript" src="http://quochung-acer/mstechnology.vn/Shared Documents/jquery.SPServices-0.5.6.js"></script>
<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[ID='3']").length != 1) {
                                                alert('You have permission');
        }
      }
   });
});
</script>
Step 9: Click tab Page | Stop Editing
Step 10: Login with Administrator with group ID is 3 and see result below
//More Option: Testing in sharepoint 2013 online
  <!--
        <script type="text/javascript" language="javascript" src="/sites/cms/Style%20Library/Citelum/JS/jquery-1.6.1.min.js"><span id="ms-rterangecursor-start"></span><span id="ms-rterangecursor-end"></span></script><script type="text/javascript" language="javascript" src="/sites/cms/Style%20Library/Citelum/JS/jquery.SPServices-0.6.2.min.js"></script><script type="text/javascript" language="javascript">
  $(document).ready(function() {
    //alert("jQuery");
   //alert($().SPServices.SPGetCurrentSite());
   //alert($().SPServices.SPGetCurrentUser());
   $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
      //alert(xData.responseXML.xml);
        if ($(xData.responseXML).find("Group[Name='Group Name']").length == 1) {
                    //alert('User in a group');
                    $("#ms-designer-ribbon").css("display", "none");
                    break;
                }      }
   });
  });
</script>-->

1 comment:

  1. It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. article submission sites

    ReplyDelete