Thursday, May 22, 2008

Add Images To SharePoint Publishing Site

Use the C# and the SharePoint API to add images to a publishing site.

using (SPSite siteCollection = new SPSite("http://SomeSiteCollection"))
{
using (SPWeb site = siteCollection.AllWebs["/SomeSite"])
{
SPDocumentLibrary doclib = (SPDocumentLibrary) site.Lists["Images"];

string filename = @"SomeImage.jpg";
string fileDir = @"c:\SomeImageDir";
string doclibRelPath = doclib.RootFolder.ServerRelativeUrl;
string doclibPath = siteCollection.MakeFullUrl(doclibRelPath);
string docPath = doclibPath + '\\' + filename;

Stream documentStream = new FileStream(fileDir + '\\' + filename, FileMode.Open);

SPFile file = site.Files.Add(docPath, documentStream);
file.CheckIn(string.Empty);
file.Publish(string.Empty);
file.Approve(string.Empty);

documentStream.Close();
}
}

No comments: