Sharepoint中列表项的只读按钮

时间:2011-11-22 08:49:42

标签: javascript sharepoint list

我遇到了以下Sharepoint问题:我创建了一个功能区按钮,其中显示“只读”。当我在列表中,并检查一些项目时,我想将这些项目设置为只读。 功能区按钮效果很好,当我正在做警报或其他什么时,我得到了答案。所以这不是问题所在。我做了以下事情:

var listitem;
var roleAssgn;
var Assgn;
var selectedItems;

function readonly() {
    selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var currentListGuid = SP.ListOperation.Selection.getSelectedList();

    var context = SP.ClientContext.get_current();
    var currentWeb = context.get_web();
    var currentList = currentWeb.get_lists().getById(currentListGuid);

     for (k in selectedItems) {
        listitem = currentList.getItemById(selectedItems[k].id);
        context.load(listitem, 'RoleAssignments');
        context.executeQueryAsync(Function.createDelegate(this, this.readonlyPerItem), Function.createDelegate(this, this.failed));
    }
}

function readonlyPerItem(sender, args) {
    var k;
    var Assgn;
    var r;
  context = SP.ClientContext.get_current();
  roleAssgn = listitem.get_roleAssignments();

  for(r in roleAssgn){
        Assgn = roleAssgn[r];
        alert("1");
        context.load(Assgn, 'RoleDefinitionBindings');
        alert("2");
        context.executeQueryAsync(Function.createDelegate(this, this.readonlyPerRoleA), Function.createDelegate(this, this.failed));
    }
}

function readonlyPerRoleA(sender, args) {
    var bindings = Assgn.get_roleDefinitionBindings();
    var member = Assgn.get_member();
}

function failed(sender, args) {
        alert("FAIL");
}

这很有效,直到它到达警报。 Alert-1正在运行,但不是Alert-2。调试器说:该对象不支持属性“get_ $ h”。 这发生在sp_runtime.js中:

  

SP.DataRetrievalWithExpressionString。$ 1Q_0(a.get_ $ H(),d)

我真的没有看到问题。这是一个错误还是不可能?

1 个答案:

答案 0 :(得分:0)

好的,我用另一种方式做到这一点,并想让你知道,它对我有用。我在功能区菜单中使用了JS来调用另一个网站,这只是一个空网站。我添加了参数(listguid,siteurl和itemid的逗号分隔)。 然后该网站只打印“真”或“假”。这个响应将被我的Ribbon JS捕获并显示一些消息,如果它工作与否。这是我的Ribbon JS:

<CustomAction
    Id="ReadOnlyButton"
    RegistrationId="101"
    RegistrationType="List"
    Location="CommandUI.Ribbon"
    Sequence="15"
    Rights="ManageLists"
    Title="Set Readonly">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Documents.Manage.Controls._children">
          <Button
            Id="Ribbon.Documents.ReadOnly"
            Command="ReadOnly"
            Sequence="15"
            Image16by16="/_layouts/1031/images/formatmap16x16.png"
            Image16by16Left="-80"
            Image16by16Top="-128"
            Image32by32="/_layouts/1031/images/formatmap32x32.png"
            Image32by32Left="-160"
            Image32by32Top="-256"
            Description="Read Only"
            LabelText="Read Only" 
            TemplateAlias="o1"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
         Command="ReadOnly"
         CommandAction="javascript:
           var nid;
           function getItemIds()
           {
             var itemIds = '';
             var items = SP.ListOperation.Selection.getSelectedItems();
             var item;
             for(var i in items)
             {
               item = items[i];
               if(itemIds != '')
               {
                 itemIds = itemIds + ',';
               }
               itemIds = itemIds + item.id;               
             }
             return itemIds;
           }
           function handleReadyStateChange()
           {
             if (client.readyState == 4)
             {
               if (client.status == 200) 
               {
                 SP.UI.Notify.removeNotification(nid);
                 if(client.responseText == 'True') {                  
                  nid = SP.UI.Status.addStatus('The Rights has been set successfully', '', true);
                  SP.UI.Status.setStatusPriColor(nid, 'green');
                 } else {
                  nid = SP.UI.Status.addStatus('Error while setting Rights', '', true);
                  SP.UI.Status.setStatusPriColor(nid, 'red');
                 }

                  window.setTimeout('SP.UI.Status.removeStatus(\'' + nid + '\')', 5000);
               }
             }
           }
           function invokeReadOnly()
           {
             var itemLength = 0;
             var params = 'itemids=' + getItemIds(); 
             for (var i=0;i&lt;params.length;i++) { if (',' == params.substr(i,1)) { itemLength++; } } 

             if(itemLength > 0) {
              nid = SP.UI.Notify.addNotification('Rights set for ' + (itemLength +1) + ' elements...', true);
             } else {
              nid = SP.UI.Notify.addNotification('Set Rights...', true);
             }

             var site='{SiteUrl}'; 
             var url = site + '/_layouts/ReadOnly.aspx?listId={ListId}';
             client = null;
             client = new XMLHttpRequest();
             client.onreadystatechange =  handleReadyStateChange;
             client.open('POST', url, true);         
             client.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
             client.setRequestHeader('Content-length', params.length);
             client.send(params);
           }      
           invokeReadOnly();"

      EnabledScript="javascript:
           function enableReadOnly()
           {
             var items = SP.ListOperation.Selection.getSelectedItems();
             return (items.length > 0);
           }
           enableReadOnly();"/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

这是我的网站(ReadOnly.aspx):

protected void Page_Load(object sender, EventArgs e)
{
    string itemidsAll = Page.Request["itemids"];
    string listId = Page.Request["listId"];
    bool set = true;

    if (!String.IsNullOrEmpty(itemidsAll))
    {
        string[] itemIds = itemidsAll.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        int item = 0;

        SPSite _site = null;
        SPListItem spitem = null;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            _site = new SPSite(SPContext.Current.Site.ID);
        });
        using (SPWeb web = _site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            SPList doclib = SPContext.Current.Web.Lists.GetList(new Guid(listId), false);                

            foreach (string itemId in itemIds)
            {
                if (Int32.TryParse(itemId, out item))
                {
                    spitem = doclib.GetItemById(item);
                    set &= SetItem(spitem, SPContext.Current, ref _site);
                }
            }

            web.AllowUnsafeUpdates = false;
        }
        _site.Dispose();
    }

    Response.Clear();
    Response.Write(set.ToString());
    Response.End();  
}

SetItem-Method用于设置权限。你可以在那里使用自己的东西:)