Subscribing a user to a document using a script

You can subscribe a user to notifications about document changes using the script below.

Script without PublicAPI

Add namespaces:
using EleWise.ELMA.Common.Managers;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Metadata;
using EleWise.ELMA.Model.Types.Settings;

Script text:

//load the document type
var metadata = MetadataLoader.LoadMetadata(context.Document.GetType());
//check if the user is subscribed to the document
if (WatchManager.Instance.GetWatchByUser(metadata.Uid, context.User.Id,  context.Document.Id).Count == 0)
{
  //create a new subscription if there aren't any
  WatchManager.Instance.CreateWatchByUser(metadata.Uid,  context.Document.Id, context.User);
}
 
The context.User variable has the User type and contains the user, who needs to be subscribed and the context.Doсument variable stores the document.
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
Context variables:

context.User - variable of the User type
context.Document - variable of the Document type

Namespace:

using EleWise.ELMA.API;

Script text:

PublicAPI.Services.WatchEntity.CreateWatchByUser(context.Document.TypeUid, context.Document.Id, context.User);