IDestinationChangeVersionsBuilder::AddItemMetadata

将指定的项变更添加到列表中。

HRESULT AddItemMetadata(
  const BYTE * pbOwnerReplicaId, 
  const BYTE * pbItemId, 
  const SYNC_VERSION * pChangeVersion,
  const SYNC_VERSION * pCreationVersion, 
  DWORD dwFlags,
  ISyncChangeBuilder ** ppChangeBuilder);

参数

  • pbOwnerReplicaId
    [in] pChangeVersion 和 pCreationVersion 均有效的副本的副本 ID。ID 格式必须与提供程序的 ID_PARAMETERS 结构所指定的格式相匹配。
  • pbItemId
    [in] 项的 ID。ID 格式必须与提供程序的 ID_PARAMETERS 结构所指定的格式相匹配。
  • pChangeVersion
    [in] 此变更的版本。
  • pCreationVersion
    [in] 项的创建版本。
  • ppChangeBuilder
    [out] 返回一个可用于将变更单位信息添加到变更的对象。

返回值

  • S_OK

  • E_POINTER

  • 如果 dwFlags 包含除 SYNC_CHANGE_FLAG_DELETEDSYNC_CHANGE_FLAG_DOES_NOT_EXIST 以外的任何值,则返回值为 E_INVALIDARG。

  • 如果发生无效操作,则返回值为 SYNC_E_INVALID_OPERATION。

  • 如果 pbOwnerReplicaId 或 pbItemId 的格式与提供程序的 ID 格式架构指定的格式不匹配,则返回值为 SYNC_E_ID_FORMAT_MISMATCH。

示例

以下示例说明如何枚举远程变更批中的变更,以及如何通过使用 AddItemMetadataIDestinationChangeVersionsBuilder 对象添加本地版本以创建对应的本地版本列表。本示例还使用 IDestinationChangeVersionsBuilder::GetChangeEnumerator 以返回本地版本列表。

STDMETHODIMP CMetadataMgr::GetItemBatchVersions(
    ISyncChangeBatch * pRemoteSyncChangeBatch,
    IEnumSyncChanges ** ppLocalVersionsEnum)
{
    HRESULT hr = E_UNEXPECTED;

    if (NULL == pRemoteSyncChangeBatch || NULL == ppLocalVersionsEnum)
    {
        hr = E_POINTER;
    }
    else
    {
        IProviderSyncServices* pProvSvc;
        hr = GetProviderSyncServices(&c_idParams, &pProvSvc);
        if (SUCCEEDED(hr))
        {
            IDestinationChangeVersionsBuilder* pDestChangeBuilder = NULL;
            hr = pProvSvc->CreateDestinationChangeVersionsBuilder(&pDestChangeBuilder);
            if (SUCCEEDED(hr))
            {
                IEnumSyncChanges* pRemoteEnum = NULL;
                hr = pRemoteSyncChangeBatch->GetChangeEnumerator(&pRemoteEnum);
                if (SUCCEEDED(hr))
                {
                    ULONG cFetched;

                    ISyncChange* pChange;
                    SYNC_GID gidItem;
                    DWORD cbID = sizeof(gidItem);
                    DWORD dwFlags;
                    SYNC_VERSION verCurrent;
                    SYNC_VERSION verCreation;
                    HRESULT hrEnum = S_OK;
                    while (S_OK == hrEnum && SUCCEEDED(hr))
                    {
                        pChange = NULL;
                        hrEnum = pRemoteEnum->Next(1, &pChange, &cFetched);
                        if (S_OK == hrEnum)
                        {
                            hr = pChange->GetRootItemId((BYTE*)&gidItem, &cbID);
                            if (SUCCEEDED(hr))
                            {
                                // Try to find the item in the local (destination) metadata.
                                IItemMetadata* pItem = NULL;
                                hr = FindItemMetadataByGlobalId((BYTE*)&gidItem, &pItem);
                                if (S_OK == hr)
                                {
                                    // S_OK means the item exists in our local store.
                                    // Extract its version and tombstone information.
                                    dwFlags = 0;

                                    BOOL fTombstone = FALSE;
                                    hr = pItem->GetIsDeleted(&fTombstone);
                                    if (SUCCEEDED(hr))
                                    {
                                        if (fTombstone)
                                        {
                                            dwFlags = SYNC_CHANGE_FLAG_DELETED;
                                        }
                                    }

                                    if (SUCCEEDED(hr))
                                    {
                                        hr = pItem->GetChangeVersion(&verCurrent);
                                        if (SUCCEEDED(hr))
                                        {
                                            hr = pItem->GetCreationVersion(&verCreation);                                            
                                        }
                                    }

                                    pItem->Release();
                                }
                                else if (S_FALSE == hr)
                                {
                                    // S_FALSE means this item does not exist in our local store.
                                    // Set versions to 0 and flag it as a new item.
                                    verCurrent.dwLastUpdatingReplicaKey = 0;
                                    verCurrent.ullTickCount = 0;
                                    verCreation.dwLastUpdatingReplicaKey = 0;
                                    verCreation.ullTickCount = 0;
                                    dwFlags = SYNC_CHANGE_FLAG_DOES_NOT_EXIST;
                                }

                                if (SUCCEEDED(hr))
                                {
                                    // Add the item to the batch of destination versions.
                                    GUID guidReplicaID = GUID_NULL;
                                    ULONG cbID = sizeof(guidReplicaID);
                                    hr = GetReplicaId((BYTE*)&guidReplicaID, &cbID);
                                    if (SUCCEEDED(hr))
                                    {
                                        hr = pDestChangeBuilder->AddItemMetadata((BYTE*)&guidReplicaID,
                                            (BYTE*)&gidItem, &verCurrent, &verCreation, dwFlags, NULL);
                                    }
                                }
                            }

                            pChange->Release();
                        }
                    }

                    if (FAILED(hrEnum))
                    {
                        hr = hrEnum;                    
                    }

                    pRemoteEnum->Release();                
                }

                if (SUCCEEDED(hr))
                {
                    hr = pDestChangeBuilder->GetChangeEnumerator(ppLocalVersionsEnum);               
                }

                pDestChangeBuilder->Release();
            }

            pProvSvc->Release();        
        }
    }

    return hr;
}

请参阅

参考

IDestinationChangeVersionsBuilder 接口