locked
Update User record using a C# application..... RRS feed

  • Question

  • Hi Everyone.

    I am using a LINQ query to retrieve the count of users that has a custom Boolean field set to true.

    Here it is:

      public static int GetItsmUser(IOrganizationService service)
            {
                OrganizationServiceContext context = new OrganizationServiceContext(service);
                IEnumerable<Entity> user = (from usr in context.CreateQuery("systemuser")
                            where (bool)usr["apitil_isitsmserviceuser"] == true
                            select usr);
                
                var count = user.Count();
                return count;
            }

    This works fine.

    However, I am calling this method and I am trying to set the Initiating user's custom boolean field to true.

     private void button2_Click(object sender, EventArgs e)
            {
    
                IOrganizationService service = TargetConnection.GetOrganizationService();
             
                if (GetItsmUser(service) == 0)
                {
                    Entity entity = new Entity("systemuser");
                    entity["apitil_isitsmserviceuser"]= true;
                    service.Update(entity);
                }
                    var fileprocessor = new DataFileProcessor();
                    MessageBox.Show("Import of Sample Data has just started");
                    fileprocessor.ProcessFiles(tbExportFolder.Text + "SampleData", service);
                    MessageBox.Show("Import of Sample Data Complete");
            }
    Pretty sure I am doing this incorrectly. Is there a way to do this without a plugin? I am creating an app that connects to the CRM online environment and I want to change the boolean field value of the initiating user based on a button press.



    Monday, July 29, 2013 2:52 PM

All replies

  • You need to specify the Id property of the entiy you're passing to the Update method - in this case it will have to be the systemuserid of the user that you're trying to update

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, July 29, 2013 3:01 PM
    Moderator
  • Hi.

    Thank you for your quick reply. I don't quite understand what you mean. Do you have a sample code for me to try out?

    Monday, July 29, 2013 3:12 PM
  • Hi,

    First you need to get connected to your organization. So change your button code as follows,

    private void button2_Click(object sender, EventArgs e)
            {
    IOrganizationService service = null;
                    ClientCredentials clientCredentials = new ClientCredentials();
                    clientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("user_name", "Password", "domain");
                    Uri orgServiceUri = new Uri("https://asdf.crm5.dynamics.com/XRMServices/2011/Organization.svc");
                    OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null);
                    service = (IOrganizationService)_serviceProxy;
                    MessageBox.Show("Connected to Organization");

    //write your program logic here
            }

    Change the Organization details and User credential details as yours. Also add all the .dll that you'll be adding in a plugin. Also refer SDK where you can find lot of sample code.

    If helpful, vote as helpful and mark as answer..

    Regards,

    Naveen.

    Tuesday, July 30, 2013 5:36 AM