locked
Adding retention tags to folder using EWS RRS feed

  • Question

  • Hi,

    I am trying to add retention policies to user folders programatically, using EWS

    I am taking one of the available retention tags and applying it to a custom Folder. Doing so, through code I can see that proper tag is getting applied (I am checking the value of extended property 0x3019).  However, when I open outlook, select the custom folder and click on Policy in ribbon under Folders tab, I don't see the retention tag applied. I even waited for few hours, but still tags didn't get applied.

    Can someone please suggest, what might be going wrong here?

    For reference, I have added the code snippet below. Thanks much for help.

                try

                {

                    // parameters to handle pagination in folders

                    Guid rGUID = Guid.NewGuid();

                    int pageSize = 1000;

                    int offset = 0;

                    bool moreItems = true;

                    bUserHasRecords = false;

                                 

                   

                    logMessage = "Accessing user mailbox" + userEmail + "......";

                    Console.WriteLine("Accessing user mailbox " + userEmail + "......");

                                  

                    // Set impersonated mailbox

                    ImpersonatedUserId idIEMX = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail);

                    this.serviceEWS.ImpersonatedUserId = idIEMX;

                   

                    GetUserRetentionPolicyTagsResponse rententionTags = this.serviceEWS.GetUserRetentionPolicyTags();

                    foreach (RetentionPolicyTag retentionTag in rententionTags.RetentionPolicyTags)

                    {

                        if (retentionTag.DisplayName.Equals("5 Year Delete", StringComparison.InvariantCultureIgnoreCase))

                           rGUID = retentionTag.RetentionId;                

                    }

                    ExtendedPropertyDefinition extendedPropertyRetentionTag = new ExtendedPropertyDefinition(0x3019,Microsoft.Exchange.WebServices.Data.MapiPropertyType.Binary);

                    // Get root folder handle

                    Microsoft.Exchange.WebServices.Data.Folder rootfolder = null;

                    rootfolder = Microsoft.Exchange.WebServices.Data.Folder.Bind(this.serviceEWS, WellKnownFolderName.MsgFolderRoot);

                    rootfolder.Load();

                    FolderView fv = new FolderView(pageSize, offset);

                    // Search for deep folder traversals

                    fv.Traversal = FolderTraversal.Deep;

                    fv.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName, extendedPropertyRetentionTag);

                    while (moreItems)

                    {

                        FindFoldersResults fr1 = rootfolder.FindFolders(fv);

                        // check if more items are available

                        moreItems = fr1.MoreAvailable;

                        if (moreItems)

                            fv.Offset += pageSize;

                       

                        //Loop through each folder

                        foreach (Microsoft.Exchange.WebServices.Data.Folder folder in fr1)

                        {

                            logMessage += "Accessing folder: " + folder.DisplayName + "......";                   

                           

                            if (folder.DisplayName.Equals("Unfiled", StringComparison.InvariantCultureIgnoreCase))

                            {

                                folder.SetExtendedProperty(extendedPropertyRetentionTag, rGUID.ToByteArray());

                                folder.Update();

                            }

                        

                        }

                    }               

                }

                catch (System.Exception ex)

                {

                    logMessage += ex.ToString();

                    Console.WriteLine(ex.ToString());

                    logger.WriteMessage(logMessage);

                }

     

    ashishshukla.1183

    Thursday, October 20, 2016 8:14 PM

Answers

All replies

  • If you going to set Retention policies using Extended properties then there are three properties that need to stamped your only stamping one see https://blogs.msdn.microsoft.com/akashb/2011/08/10/stamping-retention-policy-tag-using-ews-managed-api-1-1-from-powershellexchange-2010/ .

    However I would suggest you don't use the Extended property and use the Strongly Typed PolicyTag property on the Folder/Item https://msdn.microsoft.com/en-us/library/office/jj191464(v=exchg.150).aspx which is the supported way of doing this on 2013 up

    Cheers
    Glen

    Friday, October 21, 2016 5:06 AM
  • Thanks Glen for the suggestions.

    I made the below changes to the code. Code got executed without any error, but I am still unable to see the tag specified in the Outlook.

    if (folder.DisplayName.Equals("Unfiled", StringComparison.InvariantCultureIgnoreCase))

                            {

                                PolicyTag pTag = new PolicyTag(true, rGUID);

                                folder.PolicyTag = pTag;                           

                                folder.Update();

                            }

    Can you suggest, if I am doing something wrong.


    ashishshukla.1183

    Friday, October 21, 2016 3:42 PM
  • Ignore the above post.

    Policy was visible on folder after 10-15 mins. Not sure why the delay was.


    ashishshukla.1183

    Friday, October 21, 2016 3:52 PM
  • These changes take time to propagate
    Tuesday, March 3, 2020 10:01 PM