简体   繁体   中英

How to get a Sharepoint object url by guid

I'm working with SPAudit and I have an object of unknown type. Here is answer for when object is Site . But object can be any type from this enum .

I'm watching for a method that gets a GUID and returns url of specified object. Something like:

    static string GetUrlByGuid(Guid guid)
    {
        var item = SPFarm.Local.GetObject(guid);
        if (item == null) 
            return null;
        return item.ToString(); //return item.Url or something like it
    }

You could utilize SPAuditEntry.DocLocation Property to get the location of an audited object at the time of the audited event.

Example

var query = new SPAuditQuery(site);
query.SetRangeStart(DateTime.Now.AddHours(-36));
var entries = site.Audit.GetEntries(query);
foreach (SPAuditEntry entry in entries)
{
    Console.WriteLine(entry.DocLocation);
}

Well my solution is not really very good, bcs for lists and listitems it requires location string (DocLocation property from SPAudit). But at least, it works.

    private static string GetUrlByGuid(Guid guid, SPAuditItemType type, string location)
    {
        switch (type)
        {
            case SPAuditItemType.Site:
                return SPContext.Current.Site.Url;
            case SPAuditItemType.Web:
                try
                {
                    using (var site = new SPSite(SPContext.Current.Site.ID))
                    using (var web = site.OpenWeb(guid))
                    {
                        return web.Url;
                    }
                }
                catch (FileNotFoundException)
                {
                    return string.Empty;
                }
            case SPAuditItemType.List:
            {
                if (string.IsNullOrEmpty(location))
                    throw new ArgumentNullException("location");
                using (var site = new SPSite(SPContext.Current.Site.Url + "/" + location))
                {
                    using (var web = site.OpenWeb())
                    {
                        try
                        {
                            return web.Lists[guid].DefaultViewUrl;
                        }
                        catch (SPException)
                        {
                            return string.Empty;
                        }
                    }
                }
            }
            case SPAuditItemType.ListItem:
                var match = ListItemRegex.Match(location);
                string listUrl = match.Groups[1].Value.Trim('/');
                using (var site = new SPSite(SPContext.Current.Site.Url + "/" + location))
                using (var web = site.OpenWeb()) 
                {
                    foreach (SPList list in web.Lists)
                    {
                        if (list.RootFolder.ServerRelativeUrl.Trim('/') == listUrl)
                        {
                            return string.Format("{0}?ID={1}",
                                SPUtility.ConcatUrls(web.Url, list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url),
                                match.Groups[2].Value);
                        }
                    }
                }
                return string.Empty;
            case SPAuditItemType.Document:
                return SPContext.Current.Site.Url + "/" + location;
            default:
                return string.Empty;

        }
    }

    private static readonly Regex ListItemRegex = new Regex(@"(.+?)(\d+)_.000", RegexOptions.Compiled);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM