Hi guys here with this code you can link the files in the sharepoint document library to shapoint list fields. So in the list item, document will be diplayed as a hyperlink.
In this example I have linked a file to a discussions list in community site (Sharepoint 2013).
public void linkFile()
{
using (SPSite site = new SPSite("http://site-url"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder rootFolder = web.Folders["SiteAssets"].SubFolders["SitePages"].SubFolders["NewFolder"];
SPFile file = rootFolder.Files["sharepoint-coding.pdf"];
SPList list = web.Lists["Discussions List"];
SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);
SPList Item item = list.Items.Add();
item["Title"] = "Testing post to link attachment";
item["Body"] = "<p><strong>Attachments</strong></p>" + "<p><a href=\"/" + file.Url + "\"><img src=\"/_layouts/15/images/" + file.IconUrl + "\"/>" + file.Name + "</a></p>";
item.Update();
}
}
}
This is how it looks in discussions list after adding the item.
In this example I have linked a file to a discussions list in community site (Sharepoint 2013).
public void linkFile()
{
using (SPSite site = new SPSite("http://site-url"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder rootFolder = web.Folders["SiteAssets"].SubFolders["SitePages"].SubFolders["NewFolder"];
SPFile file = rootFolder.Files["sharepoint-coding.pdf"];
SPList list = web.Lists["Discussions List"];
SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);
SPList Item item = list.Items.Add();
item["Title"] = "Testing post to link attachment";
item["Body"] = "<p><strong>Attachments</strong></p>" + "<p><a href=\"/" + file.Url + "\"><img src=\"/_layouts/15/images/" + file.IconUrl + "\"/>" + file.Name + "</a></p>";
item.Update();
}
}
}
This is how it looks in discussions list after adding the item.
Comment your questions and suggestions
Cheers Guys.
Luckshan Roy.
No comments:
Post a Comment