
Here are some code snippets to help you out when dealing with replicated environments.
Finding out your local workgroup
One of the first things you will probably want to do is figure out which workgroup you are connected to. This one is pretty easy, just call GetLocalWorkgroup() in the ReplicationService.
If you want to get the list of all the workgroups connected to your local workgroup, you can call GetAllWorkgroups(). If replication is running, there should be only 1 publisher and the rest should be subscribers.
Finding out the entity's owner
The next thing you will probably want to do is figure out which workgroup owns the thing you want to work on. The best way is to use GetOwnershipByEntityId.
|
// C# |
|
' VB.NET |
For files, items and change orders you can use the "Current Owner" property, but the result is designed more for human readability. It's not very good for program logic because there are a couple of string formats that are used depending on the workgroup settings.
Error 322
This is the error you get whenever you try to do something on an Entity that you don't own. You can minimize the chance of getting this error by checking the "Locked" property on the object. However it won't hit all cases and some Entities don't have a Locked property.
First you need to detect the error. You can do this the same way you look up any other error code. There's nothing special here.
Next you need to decide how to handle the error. This one is up to you and what make sense for your application. Common options include A) displaying the error to the user and B) automatically attempt to gain ownership.
Transferring Ownership
Here is how you transfer ownership of specific Entities. You can transfer things in groups, which mean that all Entities in the group transfer or none do at all. This feature is useful if you have a group of things that you need to work on.
If you are transferring multiple groups, it's possible for one group to fail while another succeeds. So you need to check the XferStatus to make sure the transfer succeeded.
|
// transfer a file and item together XferStatus [] status = if (status.Any(n => !n.IsSuccess)) |
|
' transfer a file and item together Dim status As XferStatus() = replSvc.TransferEntityGroupOwnership(destWorkgroup.Id, New LongArray() {group}, 60) If status.Any(Function(n) Not n.IsSuccess) Then |
Using Newly Transferred Entities
So your code just transferred file ownership to your local workgroup. You can start working on it, right? Wrong!
You need to wait for SQL to replicate the Entity data before you can start using. The reason is because the Transfer function you called edited data on the remote workgroup, not your local workgroup. Every other API function that edits data does the edit on the local database, but the Transfer functions are special.
So how do know when you have ownership for real? The only way is to constantly check the ownership until it shows that your local workgroup owns it.
|
status = replSvc.TransferEntityGroupOwnership // TODO: check status // check the ownership. Give up after 120 seconds. |
|
status = replSvc.TransferEntityGroupOwnership(localWorkgroup.Id, New LongArray() {group}, 0) ' TOOD: check status ' check the ownership. Give up after 120 seconds. If Not ownerhips(0).IsLocalOwn Then If Not ownerhips(0).IsLocalOwn Then |
Note: Make sure that your loop has an exit. If the network goes down between the two workgroups, your program would be halted indefinitely.

Leave a Reply