Skip to content

Instantly share code, notes, and snippets.

@jjaramillo
Created June 16, 2015 17:12
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 jjaramillo/5605d069de25753005f7 to your computer and use it in GitHub Desktop.
Save jjaramillo/5605d069de25753005f7 to your computer and use it in GitHub Desktop.
Creating a new document from a already existing document inside of sharepoint
public static string SaveDocument(string docName, Guid libraryID, string templatePath)
{
string newDocPath = string.Empty;
byte[] template = null;
string fileExtension = string.Empty;
string fileName = string.Empty;
string templateName = string.Empty;
string recordsSiteUrl = ConfigurationHelper.GetInstance().ConfigItems[GlobalConstants.SITE_RECORDS_URL];
string templateFileExtension = templatePath.Substring(templatePath.LastIndexOf('.') + 1);
string libraryName = SPContext.Current.Web.CurrentUser.LoginName;
libraryName = libraryName.Substring(libraryName.LastIndexOf('\\') + 1);
fileExtension = GetFileExtension(templatePath);
fileName = string.Format("{0}.{1}", docName.TrimStart(), fileExtension);
using (SPSite recordsSite = new SPSite(recordsSiteUrl))
{
using (SPWeb recordsSiteWeb = recordsSite.OpenWeb())
{
SPFile file = recordsSiteWeb.GetFile(templatePath);
template = file.OpenBinary();
templateName = file.Name;
}
}
byte[] documentData = default(byte[]);
using (MemoryStream stream = new MemoryStream(template))
{
using (var document = WordprocessingDocument.Open(stream, true))
{
// delete from custom properties first
if (document.CustomFilePropertiesPart != null && document.CustomFilePropertiesPart.Properties != null)
{
document.CustomFilePropertiesPart.Properties.RemoveAllChildren();
document.CustomFilePropertiesPart.Properties.Save();
}
// then from custom xml part "properties"
if (document.MainDocumentPart != null && document.MainDocumentPart.CustomXmlParts != null)
{
Func<CustomXmlPart, bool> predicate =
p =>
{
using (var reader = new StreamReader(p.GetStream()))
{
var root = XElement.Load(reader);
return (root.Name.LocalName == "properties");
}
};
var propertiesPart = document.MainDocumentPart.CustomXmlParts.FirstOrDefault(p => predicate(p));
if (propertiesPart != null)
{
document.MainDocumentPart.DeletePart(propertiesPart);
}
}
}
documentData = new byte[stream.Length];
stream.Position = 0;
stream.Read(documentData, 0, Convert.ToInt32(stream.Length));
}
SPContext.Current.Web.AllowUnsafeUpdates = true;
TermSessionHelper _termSessionHelper = new TermSessionHelper(new TaxonomySession(SPContext.Current.Site));
TaxonomyFieldValue kind = _termSessionHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_TIPOS, GlobalConstants.METADATA_TERM_TIPO_EN_ELABORACION);
TaxonomyFieldValue status = _termSessionHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_STATUS, GlobalConstants.METADATA_TERM_STATUS_EN_ELABORACION);
Hashtable properties = new Hashtable();
properties.Add(CustomSiteColumns.FIELD_DOCSET_ID_RADICADO, fileName);
properties.Add("ContentType", GlobalConstants.CTTN_ARCHIVE_POXTA);
SPList targetList = SPContext.Current.Web.Lists[libraryID];
SPFileCollection listFileCollection = targetList.RootFolder.Files;
SPFile newFile = listFileCollection.Add(fileName, documentData, properties, false);
SPListItem fileItem = newFile.Item;
//newFile.Item[SPBuiltInFieldId.ContentType] = GlobalConstants.CTTN_ARCHIVE_POXTA;
TaxonomyField kindField = fileItem.Fields.GetFieldByInternalName(CustomSiteColumns.FIELD_DOCSET_KIND) as TaxonomyField;
TaxonomyField statusField = fileItem.Fields.GetFieldByInternalName(CustomSiteColumns.FIELD_DOCUMENT_STATUS) as TaxonomyField;
kindField.SetFieldValue(fileItem, kind);
statusField.SetFieldValue(fileItem, status);
//fileItem[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO] = fileName;
fileItem.Log(string.Empty, DateTime.Now, Support.Enums.TraceableOperation.Creación, SPContext.Current.Web.CurrentUser.LoginName, string.Format("Documento creado a partir de la plantilla {0}", templateName));
//newFile.ReleaseLock(newFile.LockId);
fileItem.Update();
SPListItem item = SPContext.Current.Web.GetListItem(SPContext.Current.Web.Url + "/" + newFile.Url);
item[SPBuiltInFieldId.ContentTypeId] = targetList.ContentTypes[GlobalConstants.CTTN_ARCHIVE_POXTA].Id;
item.Update();
SPContext.Current.Web.AllowUnsafeUpdates = false;
newDocPath = string.Format("{0}/{1}", SPContext.Current.Web.Url, newFile.Url);
return newDocPath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment