[70-541, #1] - Come caricare un documento in SharePoint

Ultimi giorni di preparazione per l’esame 70-540 dedicati all’uso dell’object model di SharePoint. Ovvero come scrivere il codice che effettua tutte quelle semplici operazioni che normalmente vengono effettuate dal sito SharePoint.

Cominciamo con una delle funzioni basilari: effettuare l’upload di un documento all’interno di una document library.

 

// Effettuare l’upload di un documento passando il suo nome, il folder di destinazione

// ed il documento in un array di bytes

// Il metodo restituisce l’URL al nuovo documento archiviato

public string UploadDocument(string fileName, byte[] fileContents, string targetFolder, string title)

{

      if (fileContents == null)

      {

            return "Null Attachment";

      }

      try

      {

            // il folder può essere passato nel formato:

            // http://{sito}/sites/{doclibrary}

            // ex.: "http://vmw2k3:1001/sites/DocumentCenter01"

            using (SPSite site = new SPSite(targetFolder))

            {

                  using (SPWeb web = site.OpenWeb())

                  {

                        SPFolder folder = web.GetFolder(targetFolder);

 

                        SPFile newFile = folder.Files.Add(fileName, fileContents);

                        newFile.Item["Title"] = title;

                        newFile.Item.Update();

                        return site.Url + "/" + newFile.Url;

                  }

            }

      }

      catch (System.Exception ee)

      {

            throw ee;

      }

}

 

Per trasformare un documento memorizzato sul disco in un array di byte da passare alla funzione il codice è il seguente:

 

MemoryStream stream = new MemoryStream();

FileStream fs = File.OpenRead("c:\miofile.txt");

byte[] b = new byte[fs.Length];

while (fs.Read(b, 0, b.Length) > 0)

{

      stream.Write(b, 0, b.Length);

}

fs.Close();

stream.Close();

 

Technorati Tags: , ,

posted @ giovedì 1 febbraio 2007 11:30

Print

Comments on this entry:

# re: [70-540, #1] - Come caricare un documento in SharePoint

Left by Peppe at 02/02/2007 12:42
Gravatar
Ciao. Questi post sono utilissimi.
Volevo farti solo un appunto. L'esame 70-540 è su windows mobile 5. Quello su sharepoint è il 70-541 http://www.microsoft.com/learning/exams/70-541.mspx

# re: [70-541, #1] - Come caricare un documento in SharePoint

Left by Daniele Proietti at 02/02/2007 12:54
Gravatar
Grazie per la segnalazione Peppe. Correzione effettuata ;-)
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011