Skip to content

Instantly share code, notes, and snippets.

@kobi
Created September 28, 2010 21:46
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 kobi/601861 to your computer and use it in GitHub Desktop.
Save kobi/601861 to your computer and use it in GitHub Desktop.
// 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"));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment