Skip to content

Instantly share code, notes, and snippets.

@sitefinitysteve
Created April 8, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitefinitysteve/f50db78ae3e60f0bb89b to your computer and use it in GitHub Desktop.
Save sitefinitysteve/f50db78ae3e60f0bb89b to your computer and use it in GitHub Desktop.
Sitefinity Helper to upload a file
public static Document UploadFile(string fileTitle, Stream fileStream, string fileExtension, DocumentLibrary album, Guid folderId, List<Guid> categories, List<Guid> tags, string provider = "")
{
if (album != null)
{
LibrariesManager manager = LibrariesManager.GetManager(provider);
manager.Provider.SuppressSecurityChecks = true;
string urlname = Util.UrlNameFilter(fileTitle);
Document document = null;
//Check for document
var matches = manager.GetDocuments().Where(x => x.UrlName == urlname);
//Create the document.
if (matches.Count() == 0)
{
document = manager.CreateDocument();
}
else
{
urlname += "-" + matches.Count() + 1;
}
document = manager.CreateDocument();
//Get the downloadable goods library.
if (album != null)
{
document.Parent = album;
if (folderId != Guid.Empty)
document.FolderId = folderId;
document.Title = fileTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.UrlName = urlname;
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
if (categories != null)
{
if (categories.Count() > 0)
{
foreach (var c in categories)
{
try
{
var category = taxonomyManager.GetTaxa<HierarchicalTaxon>().Where(t => t.Taxonomy.Name == "Categories").FirstOrDefault(x => x.Id == c);
if (category != null)
{
document.Organizer.AddTaxa("Category", c);
}
}
catch (Exception ex)
{
Util.SendRaygunError(ex, null);
}
}
}
}
if (tags != null)
{
if (tags.Count() > 0)
{
foreach (var t in tags)
{
try
{
var tag = taxonomyManager.GetTaxa<FlatTaxon>().Where(x => x.Taxonomy.Name == "Tags").FirstOrDefault(x => x.Id == t);
if (tag != null)
{
document.Organizer.AddTaxa("Tags", t);
}
}
catch (Exception ex)
{
Util.SendRaygunError(ex, null);
}
}
}
}
manager.Upload(document, fileStream, fileExtension);
manager.SaveChanges();
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(Telerik.Sitefinity.Libraries.Model.Document).FullName);
WorkflowManager.MessageWorkflow(document.Id, typeof(Document), null, "Publish", false, bag);
return document;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment