【问题标题】:How can I create a new "link document" on sharePoint via C#?如何通过 C# 在 sharePoint 上创建新的“链接文档”?
【发布时间】:2019-12-14 17:51:51
【问题描述】:

我在共享点中有一个文件夹,里面有很多文件(word、pp、excel 等)

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

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

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

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

谢谢

【问题讨论】:

    标签: c# asp.net sharepoint hyperlink


    【解决方案1】:

    以下代码供大家参考。

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 2021-01-28
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多