コード例 #1
0
    private string GetAttachmentUrl(DocumentAttachment attachment, DocumentAttachment mainAttachment, int versionHistoryId)
    {
        // Get link for attachment
        string attachmentUrl;

        var attName    = mainAttachment.AttachmentName;
        var documentId = mainAttachment.AttachmentDocumentID;
        var siteName   = SiteContext.CurrentSiteName;

        if (IsLiveSite && (documentId > 0))
        {
            attachmentUrl =
                ApplicationUrlHelper.ResolveUIUrl(
                    AttachmentURLProvider.GetAttachmentUrl(
                        mainAttachment.AttachmentGUID,
                        URLHelper.GetSafeFileName(attName, siteName)
                        )
                    );
        }
        else
        {
            attachmentUrl =
                ApplicationUrlHelper.ResolveUIUrl(
                    AttachmentURLProvider.GetAttachmentUrl(
                        mainAttachment.AttachmentGUID,
                        URLHelper.GetSafeFileName(attName, siteName),
                        null,
                        // Do not include version history ID for temporary attachment
                        // Version history ID may be present in case of new culture version where may be mix of temporary and version attachments from source
                        (mainAttachment.AttachmentFormGUID == Guid.Empty) ? versionHistoryId : 0
                        )
                    );
        }

        // Add variant identifier
        if (attachment.IsVariant())
        {
            attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "variant", attachment.AttachmentVariantDefinitionIdentifier);
        }

        // Ensure correct URL for non-temporary attachments, they need to be served from their original site (may be from linked documents)
        if (mainAttachment.AttachmentFormGUID == Guid.Empty)
        {
            var attachmentSiteName = SiteInfoProvider.GetSiteName(mainAttachment.AttachmentSiteID);
            if (attachmentSiteName != siteName)
            {
                attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "sitename", attachmentSiteName);
            }
        }

        attachmentUrl = URLHelper.UpdateParameterInUrl(attachmentUrl, "chset", Guid.NewGuid().ToString());

        // Add latest version requirement for live site
        if (IsLiveSite && (documentId > 0))
        {
            // Add requirement for latest version of files for current document
            string newparams = "latestfordocid=" + documentId;
            newparams += "&hash=" + ValidationHelper.GetHashString("d" + documentId);

            attachmentUrl += "&" + newparams;
        }

        return(attachmentUrl);
    }