Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Highlighted
Tuned Listener
Posts: 7
Registered: ‎01-06-2010

Error Adding a User to a Role Though sData

[ Edited ]

I wrote a little program to add users to roles though sData.  I have the following code to add a user to a role by creating a UserRole record. The below script returns the error “The remote server returned an error: (500) Internal Server”, however it does add the user to the role. I know it is not a duplicate issue since I have tested it with specific user and role Ids that were not in the system.

 

            SDataTemplateResourceRequest req = new SDataTemplateResourceRequest(_service);
            req.ResourceKind = "UserRoles";
            AtomEntry entry = req.Read();
            SDataPayload pl = entry.GetSDataPayload();
            pl.Values.Clear();
            pl.Values["Role"] = new SDataPayload { Key = strRoleId, ResourceName = "Role" };
            pl.Values["User"] = new SDataPayload { Key = Userid, ResourceName = "User" };
            entry.SetSDataPayload(pl);

            SDataSingleResourceRequest rcu = new SDataSingleResourceRequest(_service, entry);
            rcu.ResourceKind = "UserRoles";
            rcu.Entry = entry;
            AtomEntry result = rcu.Create();

 

 

I also tried to add the role by updating the User entity but that did nothing. 

            SDataSingleResourceRequest rcu = new SDataSingleResourceRequest(_service);
            rcu.ResourceKind = "Users";
            rcu.ResourceSelector = String.Format("'{0}'", Userid);
            rcu.Entry = rcu.Read();
            SDataPayload pl = rcu.Entry.GetSDataPayload();
            SDataPayloadCollection UserRoles = (SDataPayloadCollection)pl.Values["Roles"];
            pl.Values.Clear();
            SDataPayload Role = new SDataPayload { Key = strRoleId, ResourceName = "Roles" };
            UserRoles.Add(Role);

            pl.Values["Roles"] = UserRoles;
            AtomEntry result = rcu.Update(); 
        }

 

any help would be greatly appriciated!