خطأ عنوان URL غير صالح عند نشر الملفات برمجيا إلى مكتبة النمط باستخدام وحدة نمطية

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

  •  09-12-2019
  •  | 
  •  

سؤال

نستخدم ميزة لنشر الملفات الشائعة في WebParts لدينا إلى مكتبة النمط في SharePoint Server 2010، واجهنا مؤخرا المشكلة حيث لم يتم تحديث الملفات عندما نشرنا نسخة محدثة من الميزة. (ملاحظة: يمكنني استخدام مصطلح "الإصدار" فضفاضة هنا - لا نقوم بإصدار WebParts لدينا). بعد البحث حولها، وجدت أن هذه المشكلة (أي الوحدات النمطية ستنشر الإصدار الأولي من ملف ولكنها لن تقوم بتحديثها) تم توثيقها على عدد من المدونات. انتهى إبرام كل ذلك بتحديث الملفات، تحتاج إلى القيام بذلك برمجيا باستخدام مفصول. لقد وجدت اثنين من الحلول المختلفة:

للحصول على حلول webparted sandboxed:

http:// stefan- stanev-sharepoint-blog.blogspot.com.auo1/01/01/automatically-publicing-files.html

للحصول على حلول WebPart المزرعة:

href="http://johanleino.wordpress.com/2009/04/22/howto-handling-file-pdates-in-sharepoint/" rel="nofollow"> http://johanleino.wordpress .com / 2009/04/22 / Howto-معالجة الملفات - تحديثات الملفات في SharePoint /

جربت كل من الحلول، لكنه انتهى بالاستمرار في حل Johan مع بعض القرص الصغير لسيناريونا.

واجهت nullrefenceException عندما ران رمز Johan، ولكن هذا كان لأننا ليس لدينا سمة مسار على عنصر الوحدة النمطية. لقد استبدلت المكالمة لاسترداد سمة المسار مع مكالمة لاسترداد سمة URL. ثم واجهت الخطأ التالي: giveacodicetagpre.

لقد وجدت مختلف مدونات مختلفة تحدد الحلول لهذا الخطأ ولكن لا شيء قد حلها بالنسبة لي:

* ارتباطات إزالتها كما أنا ولكن مبتدئ NewBie غير قادر على نشر أكثر من 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