/// <summary>
 /// Assigns field values for specified content types to the specified matter (document library).
 /// </summary>
 /// <param name="clientContext">SP client context</param>
 /// <param name="contentTypeCollection">Collection of content types</param>
 /// <param name="matterList">List containing matters</param>
 /// <param name="matterMetadata">Object containing metadata for Matter</param>
 internal static void SetFieldValues(ClientContext clientContext, IList<ContentType> contentTypeCollection, List matterList, MatterMetadata matterMetadata)
 {
     FieldCollection fields = GetContentType(clientContext, contentTypeCollection, matterList);
     if (null != fields)
     {
         matterMetadata = ProvisionHelperFunctionsUtility.GetWSSId(clientContext, matterMetadata, fields);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientId).DefaultValue = matterMetadata.Client.Id;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientId).ReadOnlyField = true;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientId).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientId).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientName).ReadOnlyField = true;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientName).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientName).DefaultValue = matterMetadata.Client.Name;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnClientName).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterId).DefaultValue = matterMetadata.Matter.Id;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterId).ReadOnlyField = true;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterId).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterId).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterName).DefaultValue = matterMetadata.Matter.Name;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterName).ReadOnlyField = true;
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterName).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnMatterName).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnPracticeGroup).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnPracticeGroup).DefaultValue = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.MetadataDefaultValue, matterMetadata.PracticeGroupTerm.WssId, matterMetadata.PracticeGroupTerm.TermName, matterMetadata.PracticeGroupTerm.Id);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnPracticeGroup).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnAreaOfLaw).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnAreaOfLaw).DefaultValue = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.MetadataDefaultValue, matterMetadata.AreaTerm.WssId, matterMetadata.AreaTerm.TermName, matterMetadata.AreaTerm.Id);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnAreaOfLaw).Update();
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnSubareaOfLaw).SetShowInDisplayForm(true);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnSubareaOfLaw).DefaultValue = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.MetadataDefaultValue, matterMetadata.SubareaTerm.WssId, matterMetadata.SubareaTerm.TermName, matterMetadata.SubareaTerm.Id);
         fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnSubareaOfLaw).Update();
     }
 }
 /// <summary>
 /// Assigns field values for specified content types to the specified matter (document library).
 /// </summary>
 /// <param name="requestObject">Request Object containing SharePoint App Token</param>
 /// <param name="matterMetadata">Object containing metadata for Matter</param>
 /// <param name="clientContext">SP client context</param>
 /// <param name="contentTypeCollection">Collection of content types</param>
 /// <param name="client">Client Object</param>
 /// <param name="matter">Matter Object</param>
 /// <returns>true if success else false</returns>
 internal static string AssignContentTypeHelper(RequestObject requestObject, MatterMetadata matterMetadata, ClientContext clientContext, IList<ContentType> contentTypeCollection, Client client, Matter matter)
 {
     Web web = clientContext.Web;
     List matterList = web.Lists.GetByTitle(matter.Name);
     SetFieldValues(clientContext, contentTypeCollection, matterList, matterMetadata);
     clientContext.ExecuteQuery();
     SetDefaultContentType(clientContext, matterList, requestObject, client, matter);
     string[] viewColumnList = ServiceConstantStrings.ViewColumnList.Split(new string[] { ConstantStrings.Semicolon }, StringSplitOptions.RemoveEmptyEntries).Select(listEntry => listEntry.Trim()).ToArray();
     string strQuery = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.ViewOrderByQuery, ServiceConstantStrings.ViewOrderByColumn);
     bool isViewCreated = Lists.AddView(clientContext, matterList, viewColumnList, ServiceConstantStrings.ViewName, strQuery);
     return (string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, string.Empty, Convert.ToString(isViewCreated, CultureInfo.CurrentCulture).ToLower(CultureInfo.CurrentCulture)));
 }
        public string AssignContentType(RequestObject requestObject, MatterMetadata matterMetadata)
        {
            string returnValue = ConstantStrings.FALSE;
            if (null != requestObject && null != matterMetadata && null != matterMetadata.Client && null != matterMetadata.Matter && (null != requestObject.RefreshToken || null != requestObject.SPAppToken) && ValidationHelperFunctions.CheckRequestValidatorToken())
            {
                // For each value in the list of Content Type Names
                // Add that content Type to the Library
                Matter matter = matterMetadata.Matter;
                Client client = matterMetadata.Client;
                try
                {
                    using (ClientContext clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(client.Url), requestObject.RefreshToken))
                    {
                        string ProvisionMatterValidation = ValidationHelperFunctions.ProvisionMatterValidation(requestObject, client, clientContext, matter, null, int.Parse(ConstantStrings.ProvisionMatterAssignContentType, CultureInfo.InvariantCulture), null);
                        if (string.IsNullOrWhiteSpace(ProvisionMatterValidation))
                        {
                            // Returns the selected Content types from the Site Content Types
                            IList<ContentType> contentTypeCollection = SharePointHelper.GetContentTypeData(clientContext, matter.ContentTypes, requestObject, client, matter);

                            if (null != contentTypeCollection && matter.ContentTypes.Count == contentTypeCollection.Count && !string.IsNullOrWhiteSpace(matter.Name))
                            {
                                returnValue = ProvisionHelperFunctions.AssignContentTypeHelper(requestObject, matterMetadata, clientContext, contentTypeCollection, client, matter);
                            }
                            else
                            {
                                MatterCenterException customException = new MatterCenterException(TextConstants.ErrorCodeContentTypes, TextConstants.ErrorMessageContentTypes);
                                throw customException; // Throw will direct to current function's catch block (if present). If not present then it will direct to parent catch block. Parent will be the calling function
                            }
                        }
                        else
                        {
                            returnValue = ProvisionMatterValidation;
                            ProvisionHelperFunctions.DeleteMatter(requestObject, client, matter);
                        }
                    }
                }
                catch (Exception exception)
                {
                    ///// SharePoint Specific Exception
                    ProvisionHelperFunctions.DeleteMatter(requestObject, client, matter);
                    returnValue = Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
                }
            }
            else
            {
                returnValue = string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, string.Empty, TextConstants.MessageNoInputs);
            }
            return returnValue;
        }
        /// <summary>
        /// Function to get the WssID for the Practice group, Area of law and Sub area of law terms
        /// </summary>
        /// <param name="clientContext">SP client context</param>
        /// <param name="matterMetadata">Object containing meta data for Matter</param>
        /// <param name="fields">Field Collection object</param>
        /// <returns>An Object containing meta data for Matter</returns>
        internal static MatterMetadata GetWSSId(ClientContext clientContext, MatterMetadata matterMetadata, FieldCollection fields)
        {
            ClientResult<TaxonomyFieldValue> practiceGroupResult = clientContext.CastTo<TaxonomyField>
                (fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnPracticeGroup))
                .GetFieldValueAsTaxonomyFieldValue(matterMetadata.PracticeGroupTerm.Id);
            ClientResult<TaxonomyFieldValue> areaOfLawResult = clientContext.CastTo<TaxonomyField>
                (fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnAreaOfLaw))
                .GetFieldValueAsTaxonomyFieldValue(matterMetadata.AreaTerm.Id);
            ClientResult<TaxonomyFieldValue> subareaOfLawResult = clientContext.CastTo<TaxonomyField>
                (fields.GetByInternalNameOrTitle(ServiceConstantStrings.ContentTypeColumnSubareaOfLaw))
                .GetFieldValueAsTaxonomyFieldValue(matterMetadata.SubareaTerm.Id);
            clientContext.ExecuteQuery();

            matterMetadata.PracticeGroupTerm.WssId = practiceGroupResult.Value.WssId;
            matterMetadata.AreaTerm.WssId = areaOfLawResult.Value.WssId;
            matterMetadata.SubareaTerm.WssId = subareaOfLawResult.Value.WssId;
            return matterMetadata;
        }