locked
SharePoint Context Menu customizations using javascript RRS feed

  • Question

  • Hi all,

    hope someone can help me..

    I want to restrict user who can view the delete item menu in sharepoint list item.

    First I edit it through Custom_AddListMenuItem function where I copy, edit AddListMenuItem function from core.js and put it in a content editor web part.

    I put return true; in Custom_AddListMenuItem so it doesn't render the rest of AddListMenuItem function.

    However eventhough I put return true, it won't return from AddListMenuItem but continue the function and that makes the same menu shows twice.

    function Custom_AddListMenuItems(m,ctx)
    {
      if (currentItemFileUrl==null)
        currentItemFileUrl=GetAttributeFromItemTable(itemTable, "Url", "ServerUrl");
      
    	var currentItemEscapedFileUrl;
      
    	if (currentItemFileUrl !=null)
        currentItemEscapedFileUrl=escapeProperly(unescapeProperly(currentItemFileUrl));
    
      if (currentItemIsEventsExcp==null)
      {
        currentItemIsEventsExcp=false;
        currentItemIsEventsDeletedExcp=false;
        currentItemEvtType=itemTable.getAttribute("EventType");
    
        if(currentItemEvtType !=null &&(currentItemEvtType==2 || currentItemEvtType==3 || currentItemEvtType==4))
        {
          currentItemIsEventsExcp=true;
          if (currentItemEvtType==3)
            currentItemIsEventsDeletedExcp=true;
        }
      }
    
      var menuOption;
      if (ctx.listBaseType==3 && ctx.listTemplate==108)
      {
        strDisplayText=L_Reply_Text;
        if(itemTable.getAttribute("Ordering").length>=504)
        {
          var L_ReplyLimitMsg_Text="Cannot reply to this thread. The reply limit has been reached.";
          strAction="alert('"+L_ReplyLimitMsg_Text+"')";
        }
        else
        {
          strAction="STSNavigate('"+ctx.newFormUrl+"?Threading="+escapeProperly(itemTable.getAttribute("Ordering"))+"&Guid="+escapeProperly(itemTable.getAttribute("ThreadID"))+"&Subject="+escapeProperly(itemTable.getAttribute("Subject"))+"&Source="+GetSource()+"')";
        }
        strImagePath=ctx.imagesPath+"reply.gif";
        menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 100);
        menuOption.id="ID_Reply";
      }
    
      AddSharedNamespaceMenuItems(m, ctx);
    
      if (currentItemID.indexOf(".0.") < 0 && HasRights(0x0, 0x8)&& !currentItemIsEventsExcp)
      {
        if (ctx.listBaseType==4)
          strDisplayText=L_DeleteResponse_Text;
        else
          strDisplayText=L_DeleteItem_Text;
        strAction="DeleteListItem()";
        strImagePath=ctx.imagesPath+"delitem.gif";
        menuOption=custom_CAMOpt(m, strDisplayText, strAction, strImagePath, null, 300);
        menuOption.id="ID_DeleteItem";
      }
    	
      var contentTypeId=itemTable.getAttribute("CId");
      if (contentTypeId !=null && contentTypeId.indexOf("0x0106")==0 && HasRights(0x10, 0x0))
      {
        strDisplayText=L_ExportContact_Text;
        strAction="STSNavigate('"+ctx.HttpPath+"&Cmd=Display&CacheControl=1&List="+ctx.listName+"&ID="+currentItemID+"&Using="+escapeProperly(ctx.listUrlDir)+"/vcard.vcf"+"')";
        strImagePath=ctx.imagesPath+"exptitem.gif";
        menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 350);
        menuOption.id="ID_ExportContact";
      }
      CAMSep(m);
      if (ctx.verEnabled==1)
      {
        AddVersionsMenuItem(m, ctx, currentItemEscapedFileUrl);
      }
    
      AddWorkflowsMenuItem(m, ctx);
    
      CAMSep(m);
    
      if ((currentItemID.indexOf(".0.") < 0)&& HasRights(0x80, 0x0))
      {
        strDisplayText=L_Subscribe_Text;
        strAction="NavigateToSubNewAspx('"+ctx.HttpRoot+"', 'List="+ctx.listName+"&ID="+currentItemID+"')";
        strImagePath="";
        menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 1100);
        menuOption.id="ID_Subscribe";
      }
    
      if (ctx.isModerated==true && HasRights(0x0, 0x10) && HasRights(0x0, 0x4) && HasRights(0x0, 0x21000) && ctx.listBaseType !=4)
      {
        strDisplayText=L_ModerateItem_Text;
        strAction="STSNavigate('"+ctx.HttpRoot+"/_layouts/approve.aspx?List="+ctx.listName+"&ID="+currentItemID+"&Source="+GetSource()+"')";
        strImagePath=ctx.imagesPath+"apprj.gif";
        menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 1150);
        menuOption.id="ID_ModerateItem";
      }
    
      if (currentItemFSObjType==1 && ctx.ContentTypesEnabled && ctx.listTemplate !=108)
      {
        strDisplayText=L_CustomizeNewButton_Text;
        strAction="STSNavigate('"+ctx.HttpRoot+"/_layouts/ChangeContentTypeOrder.aspx?List="+ctx.listName+"&RootFolder="+currentItemEscapedFileUrl+"&Source="+GetSource()+"')";
        strImagePath="";
        menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 1170);
        menuOption.id="ID_CustomizeNewButton";
      }
    	return true;
    }<br/><br/>function custom_CAMOpt(p,wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc)<br/>{<br/>var mo=CMOpt(wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc);<br/>if(!mo)return null;<br/>//wzText you can give which one you want hide (like "Edit Properties")<br/>if(wzText != "Delete Item" || $('#hdnIsFinance').val()=="true") AChld(p,mo);<br/>return mo;<br/>}
    


    Above is my code which I put in CEWP..

    Is there something I need to add again so it will not continue the addlistmenuitem function but exit from that function?

    Thanks :)


    Best regards,
    -- Pingkan --
    Wednesday, August 10, 2011 7:07 AM

Answers

  • Hi Pingkan,

    You can restrict users to delete list items by creating a new permission level in which you have to allow users to edit items but don't allow them to delete any item.

    These are the steps you can use to create custom permission level

    • Click on Site Action > Site Settings
    • On the next page, click Advanced Permissions under the Users and Permissions heading
    • On the next page, select the Settings drop down.
    • From the list, choose Permission Levels.
    • On the next page, click the Add a Permission Level button.
    • Fill out the form on the page with a name for the permission and criteria.

    hth


    Warm Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/
    • Marked as answer by -Pingkan- Friday, August 12, 2011 3:40 AM
    Wednesday, August 10, 2011 10:03 AM

All replies

  • Hi Pingkan,

    You can restrict users to delete list items by creating a new permission level in which you have to allow users to edit items but don't allow them to delete any item.

    These are the steps you can use to create custom permission level

    • Click on Site Action > Site Settings
    • On the next page, click Advanced Permissions under the Users and Permissions heading
    • On the next page, select the Settings drop down.
    • From the list, choose Permission Levels.
    • On the next page, click the Add a Permission Level button.
    • Fill out the form on the page with a name for the permission and criteria.

    hth


    Warm Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/
    • Marked as answer by -Pingkan- Friday, August 12, 2011 3:40 AM
    Wednesday, August 10, 2011 10:03 AM
  • Hi Pratik,

    Thanks for the reply..

    I've tried to add a new permission level by copy contribute level permission and remove the delete option.

    However when I tested it, I can still delete a list item while the new permission level is applied to a whole site..

     


    Best regards,
    -- Pingkan --
    Thursday, August 11, 2011 6:35 AM
  • Hi,

    I guess you must be under site collection admin group.

    If yes please add the user who don't have any rights on the site or add a new user.


    Warm Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/
    Thursday, August 11, 2011 9:11 AM
  • Hi,

    To test it, I have a test account which is not a site collection admin..

    I use that account however the link to delete item is still on the list..


    Best regards,
    -- Pingkan --
    Thursday, August 11, 2011 9:48 AM
  • Hi Pingkan,

    from which permission you have created the custom permission ?

    I have copied from Contribute permission and created a new permission level , please make sure under List permissions uncheck "delete item" & "delete versions"  options.

    And under personal permissions do not check any option

    make sure user is not added on any other groups on site.


    Warm Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/
    • Marked as answer by -Pingkan- Friday, August 12, 2011 3:40 AM
    • Unmarked as answer by -Pingkan- Friday, August 12, 2011 3:40 AM
    Thursday, August 11, 2011 10:23 AM
  • Hi,

     

    I copied it from Contribute permissions..

    yep, I unchecked both option and unchecked all checkbox under personal permissions..

    But still the delete item is there..

     

    update: I found the issue. Turns out beside SharePoint Group, there are Domain Group to which is still in the old contribute permission. After I change it into the new one, the delete link is gone.. :) Thanks Pratik..

     

     


    Best regards,
    -- Pingkan --

     

    • Edited by -Pingkan- Friday, August 12, 2011 3:40 AM update info
    Thursday, August 11, 2011 10:28 AM
  • Hello , 

    Maybe you can send link of all masks that sharepoint use for the HasRights(requierH,requierL) function ?

    thanks

    Tuesday, July 4, 2017 3:53 PM