none
Security validation error RRS feed

  • Question

  • Dear all i have a list named sitecoll which stores all  sitecollections urls and iam looping all sitecollections and adding users to the group all sitecollections.Iam using jsom sharepoint 2010.If i use below code iam getting security validation for this page is invalid error.Please help me the same requirment how to implement in Rest api.Thanks in advance.

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
     <script>

    <html>
    <input type='text' id='name'/>
    < input type='button' id='btnadd' onclick="test()"/>Click me</button>
    </html>
     var siteUrl='';
    var deferred;
    function test() {
     
        ExecuteOrDelayUntilScriptLoaded(getvalues, "sp.js");
     
    });


     function getvalues() {  
            retrieveListItems().done(function()  
    {  
            console.log('Execute  second after the retrieve list items');   
    }).fail(function()  
    {  
     console.log('Execute  second after the retrieve list items  failed');   
    });             
        }  



     
    function retrieveListItems() {
        deferred=$.Deferred();
     
        var clientContext = new SP.ClientContext();
     
        var oList = clientContext.get_web().get_lists().getByTitle('sitecoll');
     
        var camlQuery = new SP.CamlQuery();
     
        this.collListItem = oList.getItems(camlQuery);
     
        clientContext.load(collListItem);
     
        clientContext.executeQueryAsync(
     
        Function.createDelegate(this, this.onQuerySucceeded),
     
        Function.createDelegate(this, this.onQueryFailed)
     
        );
    return deferred.promise(); 
     
    }
     
    function onQuerySucceeded(sender, args) {
     
        var listItemInfo = '';
     
        var listItemEnumerator = collListItem.getEnumerator();
       
     
        while (listItemEnumerator.moveNext()) {
     
            var oListItem = listItemEnumerator.get_current();
              siteUrl= oListItem.get_item('Url');
                AddUserToSharePointGroup();
               deferred.resolve(oListItem);
     
        }
     
        
     
    }
     
    function onQueryFailed(sender, args) {
     
        alert('Request failed. ' + args.get_message() +
     
        '\n' + args.get_stackTrace());
     
    }

    function AddUserToSharePointGroup() {  

            var clientContext = new SP.ClientContext.get_current(siteUrl);  
            var siteGroups = clientContext.get_web().get_siteGroups();  
            var group=siteGroups.getByid(3);
            var usercreationinfo=new SP.UserCreationInformation();
            usercreationinfo.setLoginName(document.getElemntById('name'))
            this.oUser=group.get_users.add(usercreationinfo)
            clientContextload(oUser);  
     

            clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);  
        }  
      
        function onQuerySucceeded() {  
            alert('success');  
        }  
      
        function onQueryFailed() {  
            alert('Request failed.');  
        }   
    </script>

    Regards,
    Praveen
    Friday, December 21, 2018 4:53 AM

Answers

  • Hi,

    SharePoint Online also have CSOM, would you please mind to use SharePoint CSOM for this as I think this will resolve Security Validation error.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Praveenrangu Thursday, December 27, 2018 9:52 AM
    Thursday, December 27, 2018 9:18 AM

All replies

  • Hi

    Please try to add formdigest to your code's above place.

    <SharePoint:FormDigest runat="server"/>

    Please refer this link.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Friday, December 21, 2018 8:12 AM
  • I have added on top of the code but no luck.please help.
    Friday, December 21, 2018 8:32 AM
  • Er..

    Please refer to this link.

    https://stackoverflow.com/questions/21092108/sharepoint-2010-the-security-validation-for-this-page-is-invalid


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Friday, December 21, 2018 9:07 AM
  • I have already checked no luck.
    Friday, December 21, 2018 9:28 AM
  • Hi,

    Both Rest API and JSOM will use current user form digest and permissions to access the target site collection, so please make sure the current user has full control permissions to all site collections which is listed in "sitecoll" list.

    And as you are using SharePoint 2010, there is no Rest API endpoint, Rest started supported in SharePoint 2013 version.

    One suggestion is that you could check all the site collections, make sure the user has been added into "Site Owners" group and then try again.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, December 24, 2018 6:41 AM
  • thanks for the reply.Iam a site collection admin and iam in owners group as well.But no luck.
    Monday, December 24, 2018 9:35 AM
  • Hi,

    Did you have permission for all the site collections listed in the "sitecoll" list ?

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, December 25, 2018 5:36 AM
  • Yes i have.
    • Edited by Praveenrangu Wednesday, December 26, 2018 9:32 AM
    Wednesday, December 26, 2018 9:32 AM
  • Hi,

    Can you use CSOM code instead of Jsom for this requirement ?

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, December 26, 2018 9:40 AM
  • I already tried with csom but the plan is they  move this to online so i have used jsom.
    Wednesday, December 26, 2018 11:08 AM
  • Hi,

    SharePoint Online also have CSOM, would you please mind to use SharePoint CSOM for this as I think this will resolve Security Validation error.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Praveenrangu Thursday, December 27, 2018 9:52 AM
    Thursday, December 27, 2018 9:18 AM