简体   繁体   English

在 featureActivated 中获取文件遇到错误:“值不在预期范围内”

[英]get file in featureActivated encounter an error : “Value does not fall within the expected range”

I set to copy a text file from a module to SharePoint server directory我设置将文本文件从模块复制到 SharePoint 服务器目录

<?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Module Name="MyModule">
    <File IgnoreIfAlreadyExists="TRUE" Path="MyModule\newFile.txt" Url="MyModule/newFile.txt" />
  </Module>
</Elements>

After that, in featureActivated, I am going to get the file by this code:之后,在 featureActivated 中,我将通过以下代码获取文件:

                try
                {
                    SPFile newFile = myWeb.GetFile(properties.Definition.RootDirectory + "\\MyModule\\newFile.txt");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

I got this exception: “Value does not fall within the expected range”.我得到了这个例外:“价值不在预期范围内”。 What is wrong with me?我是怎么了?

The SPWeb.GetFile Method (String) get file from SharePoint site, the properties.Definition.RootDirectory return the local path, you will get the exception. SPWeb.GetFile Method(String)从SharePoint站点获取文件,properties.Definition.RootDirectory返回本地路径,会得到异常。

I suggest you upload the file into document library like "Site Assets" in root web, then get file in feature receiver.我建议您将文件上传到根 web 中的“站点资产”等文档库中,然后在特征接收器中获取文件。

Example code:示例代码:

Elements.xml元素.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MyModule" Url="SiteAssets" RootWebOnly="TRUE">
    <File IgnoreIfAlreadyExists="TRUE" Type="GhostableInLibrary" Path="MyModule\newFile.txt" Url="newFile.txt" />
  </Module>
</Elements>

Feature Receiver特征接收器

try
{
    SPWeb myWeb = SPContext.Current.Site.RootWeb;
    SPFile newFile = myWeb.GetFile(myWeb.ServerRelativeUrl + "/SiteAssets/newFile.txt");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “值不在预期范围内。”窗口8 ObservableDictionary错误 - “Value does not fall within the expected range.” Window 8 ObservableDictionary Error VisualStudio ItemTemplate安装错误:值不在预期范围内 - VisualStudio ItemTemplate installation Error: Value does not fall within the expected range 价值不在预期范围内。 (运行时错误) - Value Does not fall within the expected range. (Runtime Error) 遇到错误:值不在预期范围内 - Error encountered: Value does not fall within the expected range SharePoint ListItem 错误:“值不在预期范围内” - SharePoint ListItem Error: “Value does not fall within the expected range” WinRT - 值不在预期范围内 - WinRT - Value does not fall within the expected range ArgumentException:值不在预期范围内 - ArgumentException: Value does not fall within the expected range 使用文件&#39;key.pfx&#39;中的公钥对输出进行签名时出错 - 值不在预期范围内 - Error signing output with public key from file 'key.pfx' — Value does not fall within the expected range 值不在预期范围内,孤立存储 - Value does not fall within the expected range, IsolatedStorage 值不在预期范围内 - value does not fall within expected range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM