以编程方式将用户添加到组

时间:2014-07-03 11:17:37

标签: rest sdk yammer

是否可以通过REST / sdk将用户添加到组?

场景:我们希望定期将所有用户添加到强制组中。

谢谢!
最大

3 个答案:

答案 0 :(得分:2)

您可以从共享点列表中获取组ID,如此

 function GetMandatoryGroups() {
        var context;
        var factory;
        var appContextSite;
        var oList;
        var collListItem;

        context = new SP.ClientContext(appweburl);
        factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);

        this.web = appContextSite.get_web();
        oList = this.web.get_lists().getByTitle('MandatoryGroups');
        context.load(oList);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
        collListItem = oList.getItems(camlQuery);
        context.load(collListItem, 'Include(Title,Id)');

        context.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

 function successHandler() {
            MandatoryGroups = new Array();
            var listItemInfo = '';
            var listitemenumerator = collListItem.getEnumerator();

            while (listitemenumerator.moveNext()) {
                var olistitem = listitemenumerator.get_current();
                //listItemInfo += '<li>ID:' + olistitem.get_id().toString() + ' GroupID: ' + olistitem.get_item('Title') + '</li>';
                MandatoryGroups.push(olistitem.get_item('Title'));
            }
            AutoJoinGroups();
            //  document.getElementById("message").innerHTML = 'Lists found' +   oList.get_title() + ':<ul>' + listItemInfo + '</ul>';
        }

        function errorHandler(sender, args) {
            document.getElementById("message").innerText =
                "Could not complete cross-domain call: " + args.get_message();
        }
    }

您可以将用户加入这样的mendatory组(用户登录后)

      function AutoJoinGroups() {

        yam.platform.request({
            // yam.request({
            url: "groups.json?mine=1",
            method: "GET",
            data: {},
            success: function (group) {

                //for ($i = 0; $i < MandatoryGroups.length; $i++) {
                //    if (!ArrayContains(MandatoryGroups[$i].toString(), group)) {
                //        joinGroupAsync(MandatoryGroups[$i].toString());
                //        setTimeout('', 10000);
                //       // setTimeout(joinGroupAsync(MandatoryGroups[$i].toString()),5000);
                //    } 
                //}
                var i = 0;
                function AsyncAutoJoinLoop() {
                    if (i < MandatoryGroups.length) {
                        if (!ArrayContains(MandatoryGroups[i].toString(), group)) {
                            setTimeout(function () {
                                joinGroupAsync(MandatoryGroups[i].toString());
                                i++;
                                if (i < MandatoryGroups.length) {
                                    AsyncAutoJoinLoop();
                                }
                            }, 3000)
                        }
                    }
                }
                AsyncAutoJoinLoop();
                // getMyGroups();
            },

            error: function (group) {
                console.error("There was an error with the request.");
            }
        });
    }

    function joinGroupAsync(id) {
        yam.platform.request({
            // yam.request({
            url: "group_memberships.json?group_id=" + id,
            method: "POST",
            data: {},
            success: function (group) {

            },

            error: function (group) {
                console.error("There was an error with the request.");
            }
        });
    }

您可以像这样将群组添加到共享点列表。

  function InsertMandatoryItem() {
        var context;
        var factory;
        var appContextSite;
        var oListItem;

        context = new SP.ClientContext(appweburl);
        factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);

        this.web = appContextSite.get_web();
        var oList = this.web.get_lists().getByTitle('MandatoryGroups');

        var itemCreateInfo = new SP.ListItemCreationInformation();
        oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', InsertGroupId);
        oListItem.update();

        context.load(oListItem);
        context.executeQueryAsync(
            Function.createDelegate(this, onQuerySucceeded),
            Function.createDelegate(this, onQueryFailed)
        );
        function onQuerySucceeded() {
            //alert('Item created: ' + oListItem.get_id());
            // getMyGroups();
            // AutoJoinGroups();
            $.getScript(scriptbase + 'SP.RequestExecutor.js', GetMandatoryGroups);

        }

        function onQueryFailed(sender, args) {
            $.getScript(scriptbase + 'SP.RequestExecutor.js', GetMandatoryGroups);
            alert('Request failed. ' + args.get_message() +
                '\n' + args.get_stackTrace());
        }
    }

您可以像这样

从共享点列表中删除删除组
       function RemoveMandatoryGroup() {
        var context;
        var factory;
        var appContextSite;

        var collListItem;
        var itemId;
        context = new SP.ClientContext(appweburl);
        factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);

        this.web = appContextSite.get_web();
        var oList = this.web.get_lists().getByTitle('MandatoryGroups');
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + RemoveGroupId.toString() + "</Value></Eq></Where></Query></View>");

        collListItem = oList.getItems(camlQuery);
        context.load(collListItem, 'Include(Title,Id)');


        //  this.oListItem.deleteObject();

        context.executeQueryAsync(
            Function.createDelegate(this, onQuerySucceeded),
            Function.createDelegate(this, onQueryFailed)
        );
        function onQuerySucceeded() {
            var oListItem;
            var listitemenumerator = collListItem.getEnumerator();

            while (listitemenumerator.moveNext()) {
                var itemtoDelete = listitemenumerator.get_current();
                ////listItemInfo += '<li>ID:' + olistitem.get_id().toString() + ' GroupID: ' + olistitem.get_item('Title') + '</li>';
                //MandatoryGroups.push(olistitem.get_item('Title'));
                itemId = itemtoDelete.get_id();

            }
            oListItem = oList.getItemById(itemId);
            oListItem.deleteObject();
            context.executeQueryAsync(Function.createDelegate(this, onQueryDeleteSucceeded), Function.createDelegate(this, onQueryDeleteFailed));
            //alert('Item created: ' + oListItem.get_id());
            function onQueryDeleteSucceeded() {
                //alert('Request failed. ' + args.get_message() +
                //    '\n' + args.get_stackTrace());
                getMyGroups();
                $.getScript(scriptbase + 'SP.RequestExecutor.js', GetMandatoryGroups);
            }

            function onQueryDeleteFailed() {
                alert('Request failed. ' + args.get_message() +
                    '\n' + args.get_stackTrace());
            }
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() +
                '\n' + args.get_stackTrace());
        }
    }

答案 1 :(得分:1)

不,没有。这是设计的。 Yammer喜欢用胡萝卜诱惑,而不是用棒子。我们所做的是创建通信,要求人们加入特定的群体。

api允许将当前记录的功能加入特定组。例如。在SharePoint网站上放置一个链接,其中包含&#34;加入Yammer组&#34;,并让该操作将该用户加入该组。您可以在此处查看有关如何执行此操作的详细信息:

https://developer.yammer.com/restapi/#rest-groups

答案 2 :(得分:0)

是的,在服装应用程序中,您可以在登录时自动加入该用户。我正在做的事情。