Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Can i compare if a "Person or Geoup" (column which allow multiple selection) contain the same users as inside a User Group. using server side object model

$
0
0

I have the following code inside my console application which uses the service side object model:-

using (SPSite site = new SPSite("http://SPS/"))  //site collection url
            {
                using (SPWeb web = site.OpenWeb("Customer Sites")) //specific subsite url
                {
                    SPWebCollection subWebs = web.Webs; //get all sites under specific subsites

                    foreach (SPWeb subSite in subWebs)
                    {
                        string subsitename = subSite.Title.Trim();
                        try
                        {

                            SPGroup group = web.SiteGroups[subsitename];
                            SPList managerList = subSite.Lists.TryGetList("Tracking");
                            SPFieldUserValueCollection values = new SPFieldUserValueCollection();
                            foreach (SPUser user2 in group.Users)
                            {
                                values.Add(new SPFieldUserValue(web, user2.ID, user2.Name));
                                output = output + user2.ToString();
                            }
                            foreach (SPListItem item in managerList.Items)
                            {

                                item["siteManagers"] = values;
                                item.SystemUpdate();

now what i am doing is that i will loop through all users inside a security group and then assign those users to a site column named "siteManagers".

now the above console application is scheduled to run each 2 hours, to update the items siteManagers columns. now in my above code the items are going to always be updated, regardless if the group users have been changed since the last console application run.

so to make my code more robust is there a way to  check if the

item["siteManagers"] == group.Users

before doing the update. so if the siteManagers already contain the group.Users then there is no need to do these 2 operations:-

item["siteManagers"] = values;
item.SystemUpdate();

can anyone adivce on this please?

Thanks


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>