November Happy Hour will be moved to Thursday December 5th.

Benjamin Schaefer
Apr 13, 2017
  3242
(3 votes)

Working with Episerver Social References

Episerver Social is a stand-alone platform that provides unparalleled control over the construction of models used to deliver social content. Social has two ways to uniquely reference content stored within it.

  • An Id that is generated when a model is persisted into the Social system.
  • A Reference, a means of identifying relationships between social content and application entities.

One aspect of building a maintainable social application is ensuring that you apply a consistent scheme for representing the relationships between your application and its social content. References must be capable of uniquely identifying your application's entities, being easily interpreted by your application, and robust enough to support your application's inevitable future changes.

Example use cases for References might include the following:

  • Referencing a third-party platform that Social is interacting with (for example, Episerver, Ektron, SAP, etc.)
  • Referencing a third-party provider that Social is being used with (for example, Commerce, Cms, ERP, etc.)
  • Referencing unique user Ids
  • Referencing unique product/variant Ids

To ensure the consistent construction of a reference a developer might find it advantageous to standardize the way references are created. Here are three guidelines to help you to create references.

1. Build an easily expandable mechanism for handling strings that will be used most often.

//Outline the standard platforms that will be in use with Social.
public static class Platform
{
    public const string Episerver = "Episerver";
    public const string Ektron = "Ektron";
    public const string Salesforce = "Salesforce";
    public const string SAP = "SAP";
    public const string Microsoft = "Microsoft";
}
//Outline the standard providers that exist for your platforms and will be referenced by your builder. 
public static class Provider
{ 
    //Episever and Ektron
    public const string CMS = "CMS";
    public const string Commerce = "Commerce";
 
    //Salesforce
    public const string Demandware = "Demandware";
    public const string SalesCloud = "SalesCloud";
    public const string Pardot = "Pardot";
 
    //SAP
    public const string ERP = "ERP";
    public const string Anywhere = "Anywhere";
 
    //Microsoft
    public const string Sharepoint = "Sharepoint";
    public const string Dynamics = "Dynamics";
} 

2. Build an efficient way to assemble the relevant strings needed for your Social References.

// This class builds up a string from the relevant details of a product in a third party system.
public class ProductReferenceBuilder
{
    public string Platform { get; set; }
    public string Provider { get; set; }
    public string ProductId { get; set; }
    public string Variant { get; set; }

    public ProductReferenceBuilder AddPlatform(string platform)
    {
        Platform = platform;
        return this;
    }
    
public ProductReferenceBuilder AddProvider(string provider)     {         Provider = provider;         return this;     }
public ProductReferenceBuilder AddProductId(string productId)     {         ProductId = productId;         return this;     }
public ProductReferenceBuilder AddVariantId(string variant)     {         Variant = variant;         return this;     }
  public Reference Build()     {         ProductReferenceValidation();         var referenceString = $"Product://{Platform}/{Provider}/{ProductId}/{Variant}";         return Reference.Create(referenceString);     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(ProductId) ||         string.IsNullOrWhiteSpace(Variant))         {             throw new Exception("Missing field value for ProductReference");         }     }
// This class builds up a string from the relevant details of a user in a third-party system. public class UserReferenceBuilder {     public string Platform { get; set; }     public string Provider { get; set; }     public string UserId { get; set; }
public UserReferenceBuilder AddPlatform(string platform)     {         Platform = platform;         return this;     }
  public UserReferenceBuilder AddProvider(string provider)     {         Provider = provider;         return this;     }
public UserReferenceBuilder AddUserId(string userId)     {         UserId = userId;         return this;     }
public Reference Build()     {         ProductReferenceValidation();         var referenceString = $"User://{Platform}/{Provider}/{UserId}";         return Reference.Create(referenceString);     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(UserId))         {             throw new Exception("Missing field value for UserReference");         }     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(UserId))         {             throw new Exception("Missing field value for UserReference");         }     } }

3. Add unit tests for your reference related classes to ensure that the creation of a reference matches what is expected.

[TestClass]
public class ProductBuilderTest
{
    [TestMethod]
    public void ProductReferenceBuilder_Build_ReturnsProperlyConstructedReference()
    {
         var expectedProductReference = Reference.Create("Product://Episerver/Commerce/ProductId/VarientId");
         var actualProductReference = new ProductReferenceBuilder().AddPlatform(Platform.Episerver)
                                                                   .AddProvider(Provider.Commerce)
                                                                   .AddProductId("ProductId")
                                                                   .AddVariantId("VarientId")
                                                                   .Build();
        Assert.IsTrue(expectedProductReference == actualProductReference);
    }

 

    [TestMethod]
    public void UserReferenceBuilder_Build_ReturnsProperlyConstructedReference()
    {
        var expectedProductReference = Reference.Create("User://Microsoft/Dynamics/UserId");
        var actualProductReference = new UserReferenceBuilder().AddPlatform(Platform.Microsoft)
                                                               .AddProvider(Provider.Dynamics)
                                                               .AddUserId("UserId")
                                                               .Build();
        Assert.IsTrue(expectedProductReference == actualProductReference);
    }
}

By employing these guidelines, developers can create reusable code to help construct references consistently.
Additional information on Episerver Social Reference can be found at:
http://world.episerver.com/documentation/developer-guides/social/social_platform-overview/discovering-the-platform/#referenceandIDs.
 

Apr 13, 2017

Comments

192.168.l.254 192.168.1.254
192.168.l.254 192.168.1.254 Sep 7, 2018 05:29 PM

192.168.l.254

Please login to comment.
Latest blogs
Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog