2016-07-07 48 views
0

如何獲取與當前實體關聯的所有筆記的guid列表(或收集)?獲取筆記清單

我知道它在某種程度上與service.Retreive(...)連接,但無法生成工作代碼。

附1:

public void Execute(IServiceProvider serviceProvider) 
{ 
    Entity entity = null; 

    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

    if (entity.Contains("new_parentopportunity")) 
    { 
     try 
     { 
      IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = factory.CreateOrganizationService(context.UserId); 

      Guid projectGuid = entity.Id; 
      Guid oppGuid = ((EntityReference)entity["new_parentopportunity"]).Id; 

      for (int i = 0; i < 100; i++) //Instead of 100, I'll place obtained collection.Length 
      { 
       Entity opp = service.Retrieve("", oppGuid, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); //RETREIVE ENTITY NOTE HERE 
       var temop = CreateNoteAttachment(opp); 
       service.Create(temp); 
      } 

     } 
     catch (Exception ex) 
     { 
      throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); 
     } 
    } 
} 

private Entity CreateNoteAttachment(Entity oppNote) 
{ 
    //some code here 
} 

更新1:

我也把它寫在查詢中,你可以看看這個,whethere樣樣精呢? PasteBin

+0

你有一些代碼分享?你有什麼嘗試,你哪裏錯了? – Marcus

+0

@Marcus,是的,我發佈了代碼。我不明白我應該投入什麼服務.Retreive() 此外,我已經看到了通過構造查詢的方式做到這一點 –

+0

該插件是關於從一個實體複製到另一個實體的所有筆記。 –

回答

1

兩種方式來查詢相關的注意事項:

早期綁定:

var notes = 
       service.AnnotationSet.Where(annotation => annotation.Opportunity_Annotation.Id == entity.Id) 
        .Select(annotation => annotation.Id) 
        .ToList(); 

FetchXml:

var fetchXml = string.Format("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + 
            "<entity name='annotation'>" + 
            "<attribute name='annotationid' />" + 
            "<filter type='and'>"+ 
            "<condition attribute='objectid' operator='eq' value='{0}'/>"+ 
            "</filter>"+ 
            "</entity>" + 
            "</fetch>", entity.Id); 

      var response = service.RetrieveMultiple(new FetchExpression(fetchXml)); 
      var notes = response.Entities; 
+0

謝謝,這真棒! 但是資源密集度較低的是什麼? (我想是xml的,但我不能證明這一點) –