The relationship is defined as a membership/containment relationship. So, yes, if you delete the relationship, the file attachment (and the associated blob) are deleted as well.
Based on the background you described, I'm assuming you identify a bunch of work orders whose files you want to delete if they're older than X. So, for instance "Work Items with Y team; delete file attachments older than 90 days", "Work Items
with Q team; delete file attachments older than 3 years". Is that right?
If so, you don't necessarily need to delete relationships specifically...you can simply delete the file attachment objects which will delete the relationship, the file attachment object, and the associated blob (the file itself in the database).
Using the SDK, you will not need type projections for this. You'll only need the work item object(s) (an incident or service request or whatever), the GetRelatedObjects<>() method, and an IncrementalDiscoveryData object.
So, after you get all of the appropriate work items, you can use the following snippet to delete their file attachments if the attachments meet your age requirements
//Connect to the management group
String strMySCSMServer = "<my mgmt server>";
emg = new EnterpriseManagementGroup(strMySCSMServer);
IncrementalDiscoveryData idd = new IncrementalDiscoveryData();
ManagementPackRelationship relWorkItemHasFileAttachment = emg.EntityTypes.GetRelationshipClass(new Guid("AA8C26DC-3A12-5F88-D9C7-753E5A8A55B4")); //System.WorkItemHasFileAttachment
//Get the work item's related file attachments using it's Id
Guid myWorkItemGuid = new Guid("<some work item guid>");
IList<EnterpriseManagementObject> lstFileAttachments = emg.EntityObjects.GetRelatedObjects<EnterpriseManagementObject>(myWorkItemGuid, relWorkItemHasFileAttachment, TraversalDepth.OneLevel, ObjectQueryOptions.Default);
//Loop through each file attachment
foreach (EnterpriseManagementObject emoFile in lstFileAttachments)
{
//Determine its age
DateTime AddedDate = (DateTime)emoFile[null, "AddedDate"].Value;
TimeSpan FileAttachmentAge = AddedDate.Subtract(DateTime.Now);
//Prep the file attachment for deletion if it's old enough, in this example, older than 90 days
if (FileAttachmentAge.Days > 90)
idd.Remove(emoFile);
}
//Submit the deletions to the database.
idd.Commit(emg);
-
Marked as answer by
Foothill1
Friday, January 30, 2015 9:50 PM