Wednesday, November 12, 2014

How to Add an User to SharePoint Group and send email using Javascript

Step 1 : Add the new User details into a SharePoint List "UserManagementList".

Just copy the below snippet/ code into Content Editor WebPart

<input id="newTaskButton" onclick="return false;" type="Button" text="Add User" style="background-color: #EA2B1D; color: #FFFFFF;"/>

<script type="text/javascript">

    function CreateNewItem(userName) {
        var batch =
            "<Batch OnError=\"Continue\"> \
            <Method ID=\"1\" Cmd=\"New\"> \
                <Field Name=\"UserName\">" + userName+ "</Field> \
            </Method> \
        </Batch>";

        var soapEnv =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
        <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
            xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
            xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
          <soap:Body> \
            <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
              <listName>UserManagementList</listName> \
              <updates> \
                " + batch + "</updates> \
            </UpdateListItems> \
          </soap:Body> \
        </soap:Envelope>";

        $.ajax({
            url: "Your site collection URL XXXXXXX/_vti_bin/lists.asmx",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("SOAPAction",
                "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
            },
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=utf-8"
        });
    }
    function processResult(xData, status) {
if(status =='success')
{

}
          }
    $(document).ready(function () {


        $("#newTaskButton").click(function () {


            CreateNewItem('Your SharePoint Group Name ');

 alert('The User has been added into SharePoint Groups');


}
        });


    });

 </script>
   

No comments:

Post a Comment