Thursday, August 30, 2012

SCSM Creating Manual Activity for Incident in C#


Some time ago I had to develope application that could connect two systems: Microsoft Dynamics CRM & SCSM (Microsoft System Center Service Manager). The main purpose of this application is to synchronize data between two applications.

In this article i will show you only some little part (but maybe one of the  toughest for me...) of my application code.
I will continue to post some other usefull parts from this project. 

The code consists of 3 main steps:

1. Create relationship between incident & manual activity
2. Create manual activity
3. Assign created activity to incident

There is good post about creating incident in SCSM which really helped me out: http://blogs.technet.com/b/servicemanager/archive/2009/08/09/the-system-center-platform-in-service-manager-part-3-the-data-access-service-try-it.aspx

//Create relationship between incident & manual acticvity
ManagementPackRelationship workItemContainsActivityRelationship =
mg.EntityTypes.GetRelationshipClass(new Guid("2DA498BE-0485-B2B2-D520-6EBD1698E61B"));
                                             
CreatableEnterpriseManagementRelationshipObject AssignedToIncident = 

new CreatableEnterpriseManagementRelationshipObject(mg, workItemContainsActivityRelationship);

//Create Manual Activity
CreatableEnterpriseManagementObject activityItemObject = 

new CreatableEnterpriseManagementObject(mg, classManualActivity);
Guid newActivityGuid = Guid.NewGuid();
                                             
activityItemObject[classManualActivity, "Id"].Value = newActivityGuid.ToString();                                              
activityItemObject[classManualActivity, "Title"].Value = "<Title>";
activityItemObject[classManualActivity, "Description"].Value = "<Description>";

                                             
EnterpriseManagementObjectGenericCriteria genericCr = 

new EnterpriseManagementObjectGenericCriteria("Id == '" + <SCSM Incident Id>.ToString() + "'");
IObjectReader<EnterpriseManagementObject> reader = mg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(genericCr, ObjectQueryOptions.Default);


 if (reader != null && reader.Count > 0) {
foreach (EnterpriseManagementObject obj in reader) {

AssignedToIncident.SetSource(obj);
AssignedToIncident.SetTarget(activityItemObject);

IncrementalDiscoveryData dd = new IncrementalDiscoveryData();
dd.Add(activityItemObject);
dd.Add(AssignedToIncident);
dd.Overwrite(mg);

//Assign Manual Activity to Incident
AssignedToIncident.Commit();
         }
}

Thursday, August 23, 2012

How to get all SharePoint Users with web services in c#


SPservice.UserGroup usergroup = new SPservice.UserGroup();
usergroup.Credentials = System.Net.CredentialCache.DefaultCredentials;

XmlNode allusers = usergroup.GetAllUserCollectionFromWeb();

foreach (XmlNode child in allusers.ChildNodes) {
                            foreach (XmlNode item in child) {
                                  foreach (XmlAttribute atr in item.Attributes) {
                                              if (atr.Name.ToString() == "LoginName") {
                                              MessageBox.Show(atr.OuterXml);
                                              }
                              }
                }
}

SPservice - name of web reference in your visual studio project with URL:
http://<your server name>/_vti_bin/usergroup.asmx

Friday, July 27, 2012

CRM 4 how to retrieve custom attribute value in C#

DynamicEntity someEntity;
TargetRetrieveDynamic retrieveTarget = new TargetRetrieveDynamic();

retrieveTarget.EntityId = <id of entity>;
retrieveTarget.EntityName = EntityName.<entity name>.ToString();
RetrieveRequest retrieveRequest = new RetrieveRequest();
retrieveRequest.ColumnSet = new ColumnSet(new string[] { "your_custom_attribute" });
retrieveRequest.ReturnDynamicEntities = true;
retrieveRequest.Target = retrieveTarget;

RetrieveResponse retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);
someEntity = retrieveResponse.BusinessEntity as DynamicEntity;

String result = (String)someEntity["your_custom_attribute"];
String value = result.Value.ToString();

CRM 4 Workflow error message details

In CRM 4 to get more info from workflow error than "An error has occurred" run this command against CRM database:


select Message from AsyncOperationBase
order by CreatedOn desc

Thursday, January 5, 2012

CRM 2011 Create a new Case (incident) using C#

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using System.Runtime.Serialization;
namespace CreateIncident
{
    class Program
    {
        static void Main (string[] args)
        {
            try
            {
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential
("username", "passw", "domain");

Uri organizationUri = new Uri("http://crmservername:port/orgname/XRMServices/2011/Organization.svc");
 
Uri homeRealmUri = null;
OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null); 
IOrganizationService _service = (IOrganizationService)orgService;
                              
Entity incident = new Entity(); 
incident.LogicalName = "incident";
 
incident["title"] = "Case subject..";
 
// Set customerid with some existing contact guid 
Guid customerid = new Guid("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
 
// Set customerid as contact to field "customerid" 
EntityReference CustomerId = new EntityReference("contact", customerid); 
incident["customerid"] = CustomerId;
 
// Set contactid with some existing contact guid 
Guid contactid = new Guid("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
 
// Set contactid as contact to field "casecontactid" 
EntityReference primaryContactId = new EntityReference("contact", contactid); 
incident["casecontactid"] = primaryContactId;
                                                      
_service.Create(incident);

            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
            }
        }
    }
}