如何通过C#在sharePoint上创建新的“链接文档”?

时间:2019-08-07 06:53:50

标签: c# asp.net sharepoint hyperlink

我在共享点中有一个文件夹,其中包含很多文件(word,pp,excel等)

客户端通过asp.net应用程序(c#)将文件上传到此文件夹。

现在,他们希望选择做一个“链接文档”,例如,如果有一个名为“ Test.docx”的Word文档,他们希望在尝试以类似方式上传该文件时,该文件不会被覆盖,但会创建一个新文件,该文件是指向实际文件的链接。

在sharePoint网站上,我可以选择执行“链接文件”,并使用“ aspx”后缀创建文件。

但是我没有找到如何通过代码来做到这一点。

谢谢

1 个答案:

答案 0 :(得分:0)

以下代码供您参考。

public class LinkToDocument
{

   public static readonly Guid g_guidIconOverride = new Guid("{B77CDBCF-5DCE-4937-85A7-9FC202705C91}");
   public static readonly string FLD_URL = "URL";

   /// <param name="name">file name</param>
   /// <param name="urlVal">Url del link</param>
   /// <param name="overrideIcon">file extension for the file icon</param>
   public static SPListItem CreateLinkToDocumentFile(SPDocumentLibrary list, string name, SPFieldUrlValue urlVal, string overrideIcon = null)
   {
      var docType = list.ContentTypes.Cast<SPContentType>().Single(ct => ct.Id.IsChildOf(SPBuiltInContentTypeId.LinkToDocument));
      return CreateLinkToDocumentFile(list, docType.Id, name, urlVal, overrideIcon);
   }

   public static SPListItem CreateLinkToDocumentFile(SPDocumentLibrary list, SPContentTypeId contentTypeId, string name, SPFieldUrlValue urlVal, string overrideIcon = null)
   {
      SPContentType contentType = list.ContentTypes[contentTypeId];
      string extension = "";
      if (urlVal != null && urlVal.Url != null) extension = Path.GetExtension((urlVal.Url).Trim()).TrimStart(".".ToCharArray());
      SPFolder currentFolder = list.RootFolder;
      SPFileCollection files = currentFolder.Files;
      string fileUrl = string.Concat(currentFolder.Url, "/", name, ".aspx");
      string fileTemplate = "<%@ Assembly Name='{0}' %>\r\n <%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='System.IO' %>\r\n <%@ Import Namespace='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.Utilities' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.WebControls' %>\r\n <html>\r\n <head> <meta name='progid' content='SharePoint.Link' /> </head>\r\n <body>\r\n <form id='Form1' runat='server'>\r\n <SharePoint:UrlRedirector id='Redirector1' runat='server' />\r\n </form>\r\n </body>\r\n </html>";
      StringBuilder fileContent = new StringBuilder(fileTemplate.Length + 400);
      fileContent.AppendFormat(fileTemplate, typeof(SPDocumentLibrary).Assembly.FullName);
      Hashtable properties = new Hashtable();
      SPContentTypeId ctId = contentType.Id;
      properties["ContentTypeId"] = ctId.ToString();
      SPFile file = files.Add(fileUrl, new MemoryStream((new UTF8Encoding()).GetBytes(fileContent.ToString())), properties, false, false);
      SPListItem item = file.Item;
      item[FLD_URL] = urlVal;
      if (list.Fields.Contains(g_guidIconOverride)) item[g_guidIconOverride] = string.Concat("|", overrideIcon ?? extension, "|");
      item.IconOverlay = "linkoverlay.gif";
      item.UpdateOverwriteVersion();
      return item;
   }
}

更多信息在这里:SHAREPOINT 2013: CREATE "LINK TO A DOCUMENT" PROGRAMMATICALLY

CSOM代码如下。

string docLinkTemplate = null;
using (System.IO.StreamReader sr = new System.IO.StreamReader("SharePointDocLinkTemplate.txt"))
{
    docLinkTemplate = sr.ReadToEnd();
}

string docLink = "http://myserver/test.doc";
docLinkTemplate = String.Format(docLinkTemplate, docLink);

FileCreationInformation fileCreateInfo = new FileCreationInformation();
fileCreateInfo.Content = Encoding.UTF8.GetBytes(docLinkTemplate);
fileCreateInfo.Url = "testlinkfordoc.doc.aspx";
File file = list.RootFolder.Files.Add(fileCreateInfo);
ListItem fileListItem = file.ListItemAllFields;
fileListItem["ContentType"] = "Link to a Document";
FieldUrlValue urlValue = new FieldUrlValue();
urlValue.Description = "Link to doc test";
urlValue.Url = docLink;
fileListItem["URL"] = urlValue;
context.Load(file);
context.ExecuteQuery();