모듈을 사용하여 스타일 라이브러리에 파일을 프로그래밍 방식으로 배포 할 때 잘못된 URL 오류가 잘못되었습니다.

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/49447

  •  09-12-2019
  •  | 
  •  

문제

우리는 기능을 SharePoint Server 2010의 스타일 라이브러리에있는 스타일 라이브러리에 공통적 인 파일을 배포하는 기능을 사용하여 업데이트 된 버전의 기능을 배포 할 때 파일이 업데이트되지 않는 문제를 해결했습니다. (참고 : 저는 "버전"이라는 용어를 이용하여 여기에서 선택합니다. 우리는 웹 파트를 버전 관리하지 않습니다). 주변을 검색 한 후이 문제는이 문제 (즉, 모듈이 초기 버전의 파일을 배포하지만 업데이트되지 않음)는 여러 블로그에 문서화되었습니다. 모든 파일의 결론은 파일을 업데이트하는 것이므로 FeatureReceiver를 사용하여 프로그래밍 방식으로 수행해야합니다. 나는 몇 가지 솔루션을 발견했다.

샌드 박스 웹 파트 솔루션 :

http : // stefan- stanev-sharepoint-blog.blogspot.com.au/2011/01/automally-publishing-files.html

Farm WebPart 솔루션 :

http : //johanleino.wordpress. .com / 2009 / 04 / 22 / howto-handling-file-updates-in-sharepoint /

두 솔루션을 모두 시도했지만 Johan의 해결책을 사용하여 시나리오를위한 사소한 조정으로 끝났습니다.

Johan의 코드를 실행했을 때 NullReferenceException이 발생했으나 모듈 요소에 경로 속성이 없기 때문입니다. 나는 URL 속성을 검색하기 위해 호출로 경로 속성을 검색하도록 통화를 교체했습니다. 나는 다음 오류가 발생했습니다 :

The URL ‘[url]‘ is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
.

이 오류에 대한 솔루션을 개괄하는 다양한 다른 블로그를 발견했지만 아무 것도 해결되지 않았습니다.

* 2 링크를 게시 할 수없는 낮은 초보자가 아니라 링크를 제거하십시오.) *

누구도 로그에서 제외 된 다른 제안이 있습니까?

참고 : 샌드 박스 솔루션이 아닌 농장 솔루션을 사용하고 있습니다.

도움이 되었습니까?

해결책

After doing a lot of reading and after debugging the feature receiver, I discovered two things:

  1. One of our files (which was introduced after the original version was deployed but before the current version) didn't exist in the Style Library on the SharePoint site. i.e. it had never been deployed. I checked a couple of our test servers and the file wasn't there either so this confirmed it wasn't isolated to my dev machine. This "missing" file was the file throwing the Invalid URL error when SPWeb.Files.Add was called. I confirmed that the file did exist in the hive and that the Add method could create new files. I had a look at what SPWeb.GetFile() was returning. Interestingly enough, the SPFile object returned when retrieving the "virtual file" had Exists = true... but... the file doesn't exist on the server??? (Interesting post here on SPWeb.GetFile(): http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/). I switched to using GetFileOrFolder() and casting to SPFile but my problem still wasn't resolved.

  2. After looking again at Johan's code, I decided to see if there was something else in the call to Add that could be causing an issue. I re-examined our elements.xml file to see if the entry for the "missing" file was different from the other file entries. This ended up being where my problem was... the entry for the "missing" file didn't have all of the attributes that other entries had. In particular, it was missing the Type and IgnoreIfExists attributes.

    <File Path="Style Library\Assets\Scripts\JQuery\JQueryDollarFix.js" Url="Assets/Scripts/JQuery/JQueryDollarFix.js" />
    

    I updated the entry to be:

    <File Path="Style Library\Assets\Scripts\JQuery\JQueryDollarFix.js" Url="Assets/Scripts/JQuery/JQueryDollarFix.js" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True"/>
    

    I re-deployed the wsp to the SharePoint site and activated the feature. The FeatureReceiver code ran successfully and the files were updated and checked into the Style Library.

    Feature Receiver Properties Extension (I put this in its own class in the same project as the feature):

    using System.Collections;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    
    /// <summary>
    /// Extension methods for SPFeatureReceiverProperties
    /// </summary>
    public static class SPFeatureReceiverPropertiesExtension
    {
        /// <summary>
        /// Updates the files in module.
        /// </summary>
        /// <param name="instance">The instance.</param>
        public static void UpdateFilesInModule(this SPFeatureReceiverProperties instance)
        {
            var modules = (from SPElementDefinition element in instance.Feature.Definition.GetElementDefinitions(CultureInfo.CurrentCulture)
                           where element.ElementType == "Module"
                           let xmlns = XNamespace.Get(element.XmlDefinition.NamespaceURI)
                           let module = XElement.Parse(element.XmlDefinition.OuterXml)
                           select new
                               {
                                   Url = module.Attribute("Url").Value,
                                   Path = Path.Combine(element.FeatureDefinition.RootDirectory, module.Attribute("Url").Value), // don't have a Path attribute on our Module so changed Path to URL
                                   Files = (from file in module.Elements(xmlns.GetName("File"))
                                            select new
                                                {
                                                    Url = file.Attribute("Url").Value,
                                                    Properties = (from property in file.Elements(xmlns.GetName("Property"))
                                                                  select property).ToDictionary(
                                                                      n => n.Attribute("Name").Value,
                                                                      v => v.Attribute("Value").Value)
                                                }).ToList()
                               }).ToList();
    
            using (SPWeb web = (instance.Feature.Parent as SPSite).OpenWeb())
            {
                modules.ForEach(module =>
                    {
                        module.Files.ForEach(file =>
                            {
                                string hivePath = Path.Combine(module.Path, file.Url); // in 14-hive
                                string virtualPath = string.Concat(web.Url, "/", module.Url, "/", file.Url); // in SPFS
    
                                if (File.Exists(hivePath))
                                {
                                    using (StreamReader streamReader = new StreamReader(hivePath))
                                    {
                                        object obj = web.GetFileOrFolderObject(virtualPath);
    
                                        if (obj is SPFile)
                                        {
                                            SPFile virtualFile = (SPFile)obj;
                                            bool checkOutEnabled = virtualFile.Item == null
                                                                       ? virtualFile.ParentFolder.Item.ParentList.
                                                                             ForceCheckout
                                                                       : virtualFile.Item.ParentList.ForceCheckout;
                                            bool needsApproval = virtualFile.Item == null
                                                                     ? virtualFile.ParentFolder.Item.ParentList.
                                                                           EnableModeration
                                                                     : virtualFile.Item.ParentList.EnableModeration;
    
                                            if (checkOutEnabled)
                                            {
                                                if (virtualFile.CheckOutType != SPFile.SPCheckOutType.None)
                                                {
                                                    virtualFile.UndoCheckOut();
                                                }
                                                virtualFile.CheckOut();
                                            }
    
                                            virtualFile = web.Files.Add(
                                                virtualPath,
                                                streamReader.BaseStream,
                                                new Hashtable(file.Properties),
                                                true);
    
                                            if (checkOutEnabled)
                                            {
                                                virtualFile.CheckIn(
                                                    "Checked in by deployment.", SPCheckinType.MajorCheckIn);
                                            }
    
                                            if (needsApproval)
                                            {
                                                virtualFile.Approve("Approved by deployment.");
                                            }
    
                                            virtualFile.Update();
                                        }
                                    }
                                }
                            });
                    });
            }
        }
    }
    

An excerpt of the elements.xml file for our Module:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Style Library" Url="Style Library">
    <File Path="Style Library\Assets\Scripts\JQuery\UI\jquery-ui-1.8.16.custom.js" Url="Assets/Scripts/JQuery/UI/jquery-ui-1.8.16.custom.js" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Scripts\JQuery\UI\jquery-ui-1.8.16.custom.min.js" Url="Assets/Scripts/JQuery/UI/jquery-ui-1.8.16.custom.min.js" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Resources\search.png" Url="Assets/Resources/search.png" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Scripts\Common.js" Url="Assets/Scripts/Common.js" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />   
    <File Path="Style Library\Assets\Styles\Common.css" Url="Assets/Styles/Common.css" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Styles\Pager.css" Url="Assets/Styles/Pager.css" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Styles\sharepoint.theme.overrides.css" Url="Assets/Styles/sharepoint.theme.overrides.css" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    <File Path="Style Library\Assets\Scripts\JQuery\JQueryDollarFix.js" Url="Assets/Scripts/JQuery/JQueryDollarFix.js" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True"/>
    <File Path="Style Library\Assets\Styles\images\arrow.gif" Url="Assets/Styles/images/arrow.gif" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True"/>
  </Module>
</Elements>

Useful links:

* Removed as unable to add more than 2 links *

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top