private void btnShowFiles_Click(object sender, EventArgs e) { SPFolderCollection folders = Web.Folders; foreach (SPFolder fld in folders) { Debug.WriteLine(string.Format("Folder: {0}, URL: {1} ", fld.Name, fld.ServerRelativeUrl)); foreach (SPFile f in fld.Files) { Debug.WriteLine(string.Format("File: {0}, Version: {1}.{2}", f.Name, f.MajorVersion, f.MinorVersion)); if (f.Versions.Count > 1) { SPFileVersionCollection versions = f.Versions; foreach (SPFileVersion v in versions) { Debug.WriteLine("Version: " + v.VersionLabel); Stream stm = new MemoryStream(v.OpenBinary()); ExportStream(@"d:\" + v.VersionLabel + "-" + f.Name, stm); } } } } }
private int GetPublicVersion(string url, string filepath) { int versionId = 0; //SPSecurity.RunWithElevatedPrivileges(delegate() //{ using (SPSite site = new SPSite(url)) { SPWeb web = site.OpenWeb(); SPFile file = web.GetFile(filepath); SPFileVersionCollection versions = file.Versions; foreach (SPFileVersion version in versions) { if (version.Level == SPFileLevel.Published) // && version.IsCurrentVersion) { if (versionId <= version.ID) { versionId = version.ID; } //break; } } } //}); return(versionId); }
public SPFileVersionCollectionInstance Construct(SPFileVersionCollection fileVersionCollection) { if (fileVersionCollection == null) { throw new ArgumentNullException("fileVersionCollection"); } return(new SPFileVersionCollectionInstance(this.InstancePrototype, fileVersionCollection)); }
private void GetFileContents(bool authenticated, Stream stream, HttpContext context, SPSite site, SPWeb web) { SPFile file = null; try { file = web.GetFile(this.VirtualPath); } catch { }; if (file != null && file.Exists) { try { byte[] binFile = null; string content = string.Empty; if (file.InDocumentLibrary) { SPListItem listItem = file.ListItemAllFields; int versionId = 0; bool versioning = file.DocumentLibrary.EnableVersioning; SPFileVersionCollection versions = null; if (versioning) { if (versioning && !authenticated && file.Level != SPFileLevel.Published && file.MajorVersion > 0) { versions = file.Versions; try { foreach (SPFileVersion version in versions) { if (version.Level == SPFileLevel.Published) { if (versionId <= version.ID) { versionId = version.ID; } } } } catch { } } } string url = file.ServerRelativeUrl; NameValueCollection queryString = context.Request.QueryString; StringBuilder builder = new StringBuilder(); bool NoKeys = !queryString.HasKeys(); if (versionId > 0) { bool NoVersion = true; foreach (string key in queryString.AllKeys) { if (key != null) { //NoKeys = false; var value = queryString[key]; if (key != "PageVersion") { builder.Append((NoKeys) ? "?" : "&" + key + "=" + value); } else { NoVersion = false; } } } if (NoVersion) { builder.Append((NoKeys) ? "?" : "&"); builder.Append("PageVersion=" + versionId.ToString()); url = context.Request.Url.AbsolutePath + builder.ToString(); context.RewritePath(url); } } /* * else if (file.MajorVersion > 0 && !authenticated) * { * int count = versions.Count; * * for (int i = count; i > 0; i--) * { * try * { * if (versions[i].Level == SPFileLevel.Published) * { * builder.Append((NoKeys) ? "?" : "&"); * builder.Append("PageVersion=" + versions[i].ID.ToString()); * url = context.Request.Url.AbsolutePath + builder.ToString(); * context.RewritePath(url); * break; * } * } * catch { }; * * } * } */ if (versionId == 0 && !authenticated && versioning && file.Level != SPFileLevel.Published)// && !(file.MajorVersion > 0)) { WebSiteControllerRule rule = WebSiteControllerConfig.GetRule("ErrorCode", "403"); string page = rule.Properties["ErrorPage"].ToString(); context.Response.Redirect("~/" + page + "?aspxerrorpath=" + url, false); } if (listItem != null) { PublishingPageDesignFieldValue value = null; SPFile pageLayoutFile = null; SPWeb rootWeb = null; try { if (listItem.Fields.Contains(BuildFieldId.PublishingPageDesign)) { value = listItem[BuildFieldId.PublishingPageDesign] as PublishingPageDesignFieldValue; } if (value != null) { rootWeb = site.RootWeb; if (rootWeb.ServerRelativeUrl != "/") { string rootsite = SPContext.Current.Site.RootWeb.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, string.Empty); site = new SPSite(rootsite); rootWeb = site.OpenWeb(); } pageLayoutFile = rootWeb.GetFile(value.Id); if (pageLayoutFile != null && !pageLayoutFile.Exists) { pageLayoutFile = rootWeb.GetFile(value.Url); } } } catch { }; if (pageLayoutFile != null && pageLayoutFile.Exists) { if (pageLayoutFile.Item.HasPublishedVersion && (rootWeb != null && rootWeb.Exists)) { string slayout = rootWeb.GetFileAsString(pageLayoutFile.Url); binFile = Encoding.ASCII.GetBytes(slayout); //binFile = pageLayoutFile.OpenBinary(); } else { throw (new HttpException((int)HttpStatusCode.Forbidden, this.VirtualPath)); } } else { string sfile = web.GetFileAsString(file.Url); binFile = Encoding.ASCII.GetBytes(sfile); //binFile = file.OpenBinary(); } } else { throw (new HttpException((int)HttpStatusCode.Gone, this.VirtualPath)); } } else { binFile = file.OpenBinary(); } //} //else //{ // throw (new HttpException((int)HttpStatusCode.Forbidden, "")); //} if (binFile != null && binFile.Length > 0) { MemoryStream m = new MemoryStream(binFile); StreamReader reader = new StreamReader(m); content = reader.ReadToEnd(); reader.Close(); //m.Close(); StreamWriter writer = new StreamWriter(stream); writer.Write(content); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); } } catch (HttpException) { throw; } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data); throw (new HttpException((int)HttpStatusCode.NotImplemented, "")); } finally { } } else { throw (new HttpException((int)HttpStatusCode.NotFound, this.VirtualPath)); } }
public SPFileVersionCollectionInstance(ObjectInstance prototype, SPFileVersionCollection fileVersionCollection) : this(prototype) { this.m_fileVersionCollection = fileVersionCollection; }
private void ExportHistory(string[] items, string listID) { SPTimeZone serverzone = SPContext.Current.Web.RegionalSettings.TimeZone; StringBuilder sb = new StringBuilder(); SPList list = SPContext.Current.Web.Lists[new Guid(listID)]; bool isLibrary = false; if (list.BaseType == SPBaseType.DocumentLibrary) { isLibrary = true; } HtmlTable versionTable = new HtmlTable(); versionTable.Border = 1; versionTable.CellPadding = 3; versionTable.CellSpacing = 3; HtmlTableRow htmlrow; HtmlTableCell htmlcell; // Add header row in HTML table htmlrow = new HtmlTableRow(); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Item ID"; htmlrow.Cells.Add(htmlcell); if (isLibrary) { htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "File Name"; htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Comment"; htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Size"; htmlrow.Cells.Add(htmlcell); } htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Version No."; htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Modified Date"; htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = "Modified By"; htmlrow.Cells.Add(htmlcell); foreach (SPField field in list.Fields) { if (field.ShowInVersionHistory) { htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = field.Title; htmlrow.Cells.Add(htmlcell); } } versionTable.Rows.Add(htmlrow); foreach (string item in items) { SPListItem listItem = list.GetItemById(Convert.ToInt32(item)); SPListItemVersionCollection itemVersions = listItem.Versions; SPFileVersionCollection fileVersions = null; if (isLibrary && listItem.FileSystemObjectType == SPFileSystemObjectType.File) { fileVersions = listItem.File.Versions; } for (int i = 0; i < itemVersions.Count; i++) { SPListItemVersion currentVersion = itemVersions[i]; SPListItemVersion previousVersion = itemVersions.Count > i + 1 ? itemVersions[i + 1] : null; htmlrow = new HtmlTableRow(); if (i == 0) { htmlcell = new HtmlTableCell(); htmlcell.RowSpan = itemVersions.Count; htmlcell.InnerHtml = listItem.ID.ToString(); htmlrow.Cells.Add(htmlcell); } if (isLibrary) { if (i == 0) { htmlcell = new HtmlTableCell(); htmlcell.RowSpan = itemVersions.Count; htmlcell.InnerHtml = listItem.File.Name; htmlrow.Cells.Add(htmlcell); } htmlcell = new HtmlTableCell(); HtmlTableCell sizeCell = new HtmlTableCell(); if (i == 0 && listItem.FileSystemObjectType == SPFileSystemObjectType.File) { htmlcell.InnerHtml = currentVersion.ListItem.File.CheckInComment; // Implicit conversion from long to double double bytes = currentVersion.ListItem.File.Length; sizeCell.InnerHtml = Convert.ToString(Math.Round((bytes / 1024) / 1024, 2)) + " MB"; } else { if (null != fileVersions) { foreach (SPFileVersion fileVersion in fileVersions) { if (fileVersion.VersionLabel == currentVersion.VersionLabel) { htmlcell.InnerHtml = fileVersion.CheckInComment; // Implicit conversion from long to double double bytes = fileVersion.Size; sizeCell.InnerHtml = Convert.ToString(Math.Round((bytes / 1024) / 1024, 2)) + " MB"; break; } } } } htmlrow.Cells.Add(htmlcell); htmlrow.Cells.Add(sizeCell); } htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = currentVersion.VersionLabel; htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); DateTime localDateTime = serverzone.UTCToLocalTime(currentVersion.Created); htmlcell.InnerHtml = localDateTime.ToShortDateString() + " " + localDateTime.ToLongTimeString(); htmlrow.Cells.Add(htmlcell); htmlcell = new HtmlTableCell(); htmlcell.InnerHtml = currentVersion.CreatedBy.User.Name; htmlrow.Cells.Add(htmlcell); foreach (SPField field in currentVersion.Fields) { if (field.ShowInVersionHistory) { htmlcell = new HtmlTableCell(); htmlcell.Attributes.Add("class", "textmode"); if (null != currentVersion[field.StaticName]) { if (null == previousVersion) { htmlcell.InnerHtml = GetFieldValue(field, currentVersion); } else { if (null != previousVersion[field.StaticName] && currentVersion[field.StaticName].ToString().Equals(previousVersion[field.StaticName].ToString())) { htmlcell.InnerHtml = string.Empty; } else { htmlcell.InnerHtml = GetFieldValue(field, currentVersion); } } } else { htmlcell.InnerHtml = string.Empty; } htmlrow.Cells.Add(htmlcell); } } versionTable.Rows.Add(htmlrow); } } ExportTableToExcel(versionTable, list.Title); }