コード例 #1
0
 private static void PublishAssetInternal(string assetUrl, SPListItem sourceItem)
 {
     if (!CommonHelper.IsNullOrWhiteSpace(assetUrl))
     {
         object fileSystemObj;
         try {
             int pathEndPos = assetUrl.IndexOfAny(new[] { '?', '#' });
             if (pathEndPos >= 0)
             {
                 assetUrl = assetUrl.Substring(0, pathEndPos);
             }
             fileSystemObj = sourceItem.Web.Site.GetFileOrFolder(assetUrl);
         } catch {
             return;
         }
         try {
             if (fileSystemObj is SPFolder)
             {
                 SPFolder folder = (SPFolder)fileSystemObj;
                 if (folder.ParentListId != Guid.Empty)
                 {
                     folder.EnsureApproved();
                 }
             }
             else if (fileSystemObj is SPFile)
             {
                 SPFile file = (SPFile)fileSystemObj;
                 if (file.ParentFolder.ParentListId != Guid.Empty)
                 {
                     if (file.Item != null && file.Item.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document) && !file.Item.ContentTypeId.IsChildOf(ContentTypeId.MasterPage.Parent) && !file.Item.ContentTypeId.IsChildOf(ContentTypeId.Page.Parent))
                     {
                         file.EnsurePublished(String.Concat("Publish linked asset from ", SPUrlUtility.CombineUrl(sourceItem.Web.ServerRelativeUrl, sourceItem.Url)));
                     }
                 }
             }
         } catch (Exception ex) {
             throw new Exception(String.Concat("Cannot approve or publish content at ", assetUrl), ex);
         }
     }
 }