Friday 27 September 2013

Remove all the attachments from Notes !!!

I got a requirement from one of our client, where client wants to remove all the attachments from notes entity.

Note: only attached documents not note entity record.

In my last post, I have written a function to get all the attachments form incident entity. You could get all the attachments using this method but here we would like to get all the notes regardless without attached documents. When we attach documents in notes entity, there is a field called IsDocument that field tracks any document being attached or not (true/false).

So we can add another condition into previous method like:

new ConditionExpression
{
AttributeName = "isdocument",
Operator = ConditionOperator.Equal,
Values = { true }
},


Here is the sample code to remove attachments from notes entity.

// Get all notes with attachment
EntityCollection _Notes = GetAllNotes(Id, _Service);

//Check for return results
if (_Notes.Entities.Count > 0)
{
foreach (Entity _Note in _Notes.Entities)
{
Entity _Annotation = new Entity("annotation");
_Annotation.Id = _Note.Id;

_Annotation["mimetype"] = null;
_Annotation["documentbody"] = null;
_Annotation["filename"] = null;
_Annotation["filesize"] = null;
_Annotation["isdocument"] = false;

//update the note
_Service.Update(_Annotation);
}
}

Hope this helps to someone!!!

Read all Notes related to incident !!!



Here is the sample code to real all attachments.

public static EntityCollection GetAllNotes(Guid CaseId, IOrganizationService _service)
{
EntityCollection results = null;
try
{

QueryExpression _Query = new QueryExpression
{
EntityName = "annotation",
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,

Conditions =
{
new ConditionExpression
{
AttributeName = "objectid",
Operator = ConditionOperator.Equal,
Values = { CaseId }
},
}
}
};

results = _service.RetrieveMultiple(_Query);

}
catch (Exception Ex)
{

}
return results;
}
Hope this helps to someone :) !!!