TODO:
Have you ever wanted to download a document from a document library using the SharePoint client object model?
SOLUTION:
using SP = Microsoft.SharePoint.Client;
//class and code go here
string saveLocation = "C:\\Temp\\";
//SET YOUR ITEM HERE!!!!!!
SP.ListItem item = null;
using (SP.FileInformation fileInformation = SP.File.OpenBinaryDirect(put your context here, (string)item["FileRef"]))
{
string fileName = (string)item["FileRef"];
fileName = string.Concat(saveLocation, fileName.Substring(fileName.LastIndexOf("/")+1)); //get the name of the file based on last '/'
//now save it
using (System.IO.FileStream outPutFile = System.IO.File.OpenWrite(fileName))
{
fileInformation.Stream.CopyTo(outPutFile);
fileInformation.Stream.Close();
}
}
NOTES:
There are no notes on this topic
TODO:
Have you ever wanted to add a document to a SharePoint document library? The below example, is a method that will allow you to do just that.
SOLUTION:
/// <summary>
/// Add the contents of a file to a document library
/// </summary>
/// <param name="DocumentData">byte[] of the document data</param>
/// <param name="SharePointUrl">The URL to the sharepoint server</param>
/// <param name="SharePointSite">The name of the site the document library resides</param>
/// <param name="DocumentLibrary">The name of the document library</param>
/// <param name="MetaData">Any meta data you want to add. For instance if there are other columns, you can specify them here.</param>
/// <param name="OverWriteFile">Overwrite flag. true or false, self explanatory</param>
/// <returns></returns>
public bool AddFileToDocumentLibrary(byte[] DocumentData, string SharePointUrl, string SharePointSite, string DocumentLibrary, Hashtable MetaData, bool OverWriteFile)
{
try
{
//get the site
using (SPSite mysiteCollection = new SPSite(SharePointUrl))
{
//get web info by title (i need to do it this way)
SPWebInfo webinfo = mysiteCollection.AllWebs.WebsInfo.Find(delegate(SPWebInfo w) { return w.Title.ToUpper() == SharePointSite.ToUpper(); });
//get the site by title (need to because name could be site/site...)
using (SPWeb rootSite = mysiteCollection.AllWebs[webinfo.Id]) //now get the web by GUID
{
SPFile newFile = null;
SPFolder incompleteLibrary = rootSite.Folders[DocumentLibrary]; //the list by display name
//now create a hash of our fields we want to set, and do it in one shot.
if (MetaData == null || MetaData.Count == 0)
newFile = incompleteLibrary.Files.Add(string.Format("{0}.xml", Guid.NewGuid()), DocumentData, OverWriteFile); //add it to the library
else
newFile = incompleteLibrary.Files.Add(string.Format("{0}.xml", Guid.NewGuid()), DocumentData, MetaData, OverWriteFile); //add it to the library
}
}
return true;
}
catch (Exception x)
{
return false;
}
}
NOTES:
In my case, I was only adding XML files. Therefore you can change the file name generation to use an extension that is passed in potentially.
TODO:
Have you ever created an Event Receiver, and you would like to have it fire for all items in a list? To do this, I created a Feature, that upon activation, will open a list or document library, and call Update() on each item in the list. That will cause the Event Receiver to fire, thus applying its logic to each item in the list.
SOLUTION:
/// <summary>
/// Feature activated method
/// </summary>
/// <param name="properties"></param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
//check the context
if (SPContext.Current == null || SPContext.Current.Web == null || string.IsNullOrEmpty(SPContext.Current.Web.Url))
return;
// Use using to make sure resources are released properly
using (SPSite mysiteCollection = new SPSite(SPContext.Current.Web.Url))
{
// Alternately you can use oSite.RootWeb if you want to access the main site
SPWebInfo webinfo = mysiteCollection.AllWebs.WebsInfo.Find(delegate(SPWebInfo w) { return w.Title == "My Site Name"; }); //get the site by title (need to because name could be site/site)
SPWeb rootSite = mysiteCollection.AllWebs[webinfo.Id]; //now get the web by GUID
SPList oList = rootSite.Lists["My List Name"]; //the list by display name
foreach (SPListItem oItem in InputList.Items)
{
//now just update it, so the event reciever fires
oItem.Update();
}
}
}
catch (Exception x)
{
}
}
NOTES:
There are no notes on this topic