CRM 2011: Get List of Publishers using C#

A method which returns the list of publishers available on the Microsoft Dynamics CRM 2011 system.

public static List<Publisher> GetPublishersList(IOrganizationService OrgService)
{
    QueryExpression query = new QueryExpression()
    {
        EntityName = "publisher",
        ColumnSet = new ColumnSet(true),
        Criteria = new FilterExpression()
    };

    EntityCollection result = OrgService.RetrieveMultiple(query);
    List<Publisher> PublisherList = new List<Publisher>();

    foreach (var publisher in result.Entities)
    {
        PublisherList.Add(
            new Publisher()
            {
                FriendlyName = publisher["friendlyname"].ToString(),
                OrganizationId = ((EntityReference)publisher["organizationid"]).Id,
                PublisherId = (Guid)publisher["publisherid"],
                UniqueName = publisher["uniquename"].ToString(),
                CustomizationPrefix = publisher["customizationprefix"].ToString(),
                IsReadOnly = (bool)publisher["isreadonly"]
            });
    }

    return PublisherList;
}

And a class Publisher is defined as:

/// <summary>
/// Publisher class
/// </summary>
public class Publisher
{
    /// <summary>
    /// Gets or sets the organization id.
    /// </summary>
    /// <value>
    /// The organization id.
    /// </value>
    public Guid OrganizationId { get; set; }

    /// <summary>
    /// Gets or sets the name of the unique.
    /// </summary>
    /// <value>
    /// The name of the unique.
    /// </value>
    public string UniqueName { get; set; }

    /// <summary>
    /// Gets or sets the publisher id.
    /// </summary>
    /// <value>
    /// The publisher id.
    /// </value>
    public Guid PublisherId { get; set; }

    /// <summary>
    /// Gets or sets the customization prefix.
    /// </summary>
    /// <value>
    /// The customization prefix.
    /// </value>
    public string CustomizationPrefix { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is read only.
    /// </summary>
    /// <value>
    /// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
    /// </value>
    public bool IsReadOnly { get; set; }

    /// <summary>
    /// Gets or sets the name of the friendly.
    /// </summary>
    /// <value>
    /// The name of the friendly.
    /// </value>
    public string FriendlyName { get; set; }
}

I want to specify that this code is used when CRM service proxy is not created in the project. You need to create an IOrganizationService object when you  have connected to the CRM 2011 system and passing it to the method I have  provided above.

Happy Programming!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s