public
Created

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
// Source code of DocIdRedir.aspx
// Microsoft.Office.DocumentManagement.Pages
// DLL Folder: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\BIN\Microsoft.Office.DocumentManagement.Pages.dll
// Used for http://kobikobi.wordpress.com/2010/09/29/sharepoint-2010-using-document-id-to-link-to-a-specific-version/
 
[PermissionSet(SecurityAction.Demand, Name="FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust")]
public class DocIdRedir : LayoutsPageBase
{
// Methods
private static string GetSearchUrl(SPWeb web, string docId)
{
string str = "OSSSearchResults.aspx";
string searchScope = DocIdLookup.GetSearchScope(web.Site);
if (string.IsNullOrEmpty(searchScope))
{
try
{
SPServiceContext current = SPServiceContext.Current;
if (current != null)
{
RemoteScopes scopes = new RemoteScopes(current);
Scope scope = scopes.AllScopes[1];
searchScope = scope.Name;
}
}
catch (KeyNotFoundException)
{
ULS.SendTraceTag(0x67323361, ULSCat.msoulscat_DLC_DM, ULSTraceLevel.High, "The All Sites scope does not exist.");
}
}
docId = (DocIdLookup.IsFastSearch(web.Site) ? "spdocid=" : "docid=") + docId;
if (searchScope != null)
{
return string.Format(CultureInfo.InvariantCulture, "{0}?k={1}&s={2}", new object[] { str, SPHttpUtility.UrlKeyValueEncode(docId), SPHttpUtility.UrlKeyValueEncode(searchScope) });
}
return string.Format(CultureInfo.InvariantCulture, "{0}?k={1}", new object[] { str, SPHttpUtility.UrlKeyValueEncode(docId) });
}
 
private static string GetWacUrl(SPListItem spListItem)
{
SPList parentList = spListItem.ParentList;
if ((parentList.DefaultItemOpen == DefaultItemOpen.PreferClient) || (spListItem.File == null))
{
return string.Empty;
}
string strFileName = SPUrlUtility.CombineUrl(parentList.ParentWeb.ServerRelativeUrl, spListItem.Url);
return SPUtility.MapToServerFileRedirect(parentList.ParentWeb, strFileName, spListItem.File.ProgID);
}
 
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string str = base.Request.QueryString["ID"];
if (string.IsNullOrEmpty(str))
{
SPUtility.TransferToErrorPage((string) base.GetGlobalResourceObject("dlcdm", "DocIdSettings_Error_InvalidArguments"));
}
else
{
bool flag = false;
SPSite contextSite = SPControl.GetContextSite(this.Context);
if (!DocumentId.IsFeatureEnabled(contextSite))
{
SPUtility.TransferToErrorPage((string) base.GetGlobalResourceObject("dlcdm", "DocIdSettings_Error_NotEnabled"));
}
else
{
string str2 = base.Request.QueryString["hintUrl"];
string[] strArray = null;
if (!string.IsNullOrEmpty(str2))
{
bool flag2 = false;
SPWeb contextWeb = null;
try
{
contextWeb = SPControl.GetContextWeb(this.Context);
string strA = contextWeb.GetFile(str2).Item["_dlc_DocId"] as string;
flag2 = string.Compare(strA, str, StringComparison.OrdinalIgnoreCase) == 0;
}
catch
{
}
if (flag2)
{
strArray = new string[] { UrlUtility.EnsureTrailingSlash(contextWeb.Url) + str2 };
}
}
if ((strArray == null) || (strArray.Length == 0))
{
try
{
strArray = DocumentId.FindUrlsById(contextSite, str);
}
catch (Exception exception)
{
SPUtility.TransferToErrorPage(string.Format(CultureInfo.InvariantCulture, (string) base.GetGlobalResourceObject("dlcdm", "DocIdSettings_Error_Lookup_Exception_Format_2"), new object[] { str, exception.Message }));
}
}
if ((strArray == null) || (strArray.Length == 0))
{
SPUtility.TransferToSuccessPage(string.Format(CultureInfo.InvariantCulture, (string) base.GetGlobalResourceObject("dlcdm", "DocIdSettings_Error_NotFound"), new object[] { str }));
}
else if (strArray.Length == 1)
{
string str4 = strArray[0];
if (string.IsNullOrEmpty(str4))
{
flag = true;
}
else if (!str4.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
{
try
{
using (SPSite site2 = new SPSite(str4))
{
using (SPWeb web2 = site2.OpenWeb())
{
SPFile file = web2.GetFile(str4);
if (file != null)
{
SPListItem spListItem = file.Item;
if (spListItem != null)
{
string wacUrl = GetWacUrl(spListItem);
if (!string.IsNullOrEmpty(wacUrl))
{
str4 = wacUrl;
}
}
}
}
}
}
catch
{
}
}
if (!flag)
{
SPUtility.Redirect(str4, SPRedirectFlags.Static, this.Context);
}
}
else
{
SPUtility.Redirect(GetSearchUrl(SPControl.GetContextWeb(this.Context), str), SPRedirectFlags.RelativeToLayoutsPage, this.Context);
}
if (flag)
{
SPUtility.TransferToErrorPage((string) base.GetGlobalResourceObject("dlcdm", "DocIdSettings_Error_Unexpected"));
}
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.