SharePoint 2007 - Come creare un link ad un documento

Ho avuto la necessità di creare, all’interno di una “document library”, un link ad un documento contenuto in un altro percorso. Tale funzionalità è supportata nativamente da SharePoint tramite il Content-Type “Link to a document”, tuttavia non sono riuscito a trovare nessuna documentazione su come fosse possibile ottenere questo risultato da codice. Ho scoperto che le librerie di SharePoint non espongono nessun metodo per ottenere questo risultato, e che l’unica cosa che viene fatta è la creazione a Run-Time di un file .aspx che contiene un UrlRedirector. La cosa che assolutamente non mi piace è il fatto che, il contenuto del file .aspx creato, è scolpito nel codice e non memorizzato su un file esterno utilizzato come template.

Per ottenere il mio scopo è stato comunque sufficiente applicare la stessa strategia utilizzata da MS ed il gioco è fatto:

 

string linkUrl = "http://myServer:1001/sites/mySite/Documents";

string documentToLink = "http://myServer:1001/sites/mySite/Documents/myDoc.doc";

string linkName = "nome del link";

SPSite site = new SPSite(linkUrl);

SPWeb web = site.OpenWeb();

 

SPContentType ct = web.ContentTypes["Link to a Document"];

SPFolder myFolder = web.GetFolder(linkUrl);

SPFileCollection myCollection = myFolder.Files;

string linkToDoc = myFolder.Url + "/" + linkName + ".aspx";

string fileText = ""|%@ 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'>\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 sBuilder = new StringBuilder(fileText.Length + 400);

sBuilder.AppendFormat(fileText, typeof(SPDocumentLibrary).Assembly.FullName);

SPFile newFile = collection1.Add(linkToDoc, new UTF8Encoding().GetBytes(sBuilder.ToString()), false);

SPListItem newItem = newFile.Item;

newItem["ContentType"] = ct.Name;

newItem["URL"] = documentToLink + ", ";

newItem.UpdateOverwriteVersion();

if (newFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)

{

      newFile.CheckIn("");

}

N.B.: Nella riga dove viene definito il contenuto del fileText va sostituito il carattere | con il carattere <

 

Technorati Tags: ,

posted @ venerdì 27 ottobre 2006 18:03

Print

Comments on this entry:

# re: SharePoint 2007 - Come creare un link ad un documento

Left by Daniele Proietti at 24/11/2006 14:24
Gravatar
Omar hai perfettamente ragione. Questo tipo di implementazione dipende esclusivamente dal fatto che sto lavorando con la Beta 2, quindi prima di approntare una mia soluzione definitiva voglio vedere quale sarà l'approccio di Microsoft nella versione ufficiale.

# re: SharePoint 2007 - Come creare un link ad un documento

Left by Omar Damiani at 24/11/2006 19:16
Gravatar
Ah, ok allora...volevo solo capire perchè non mi tornavano le cose...:)
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011