Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10015

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
    }

Upvotes: 1

Views: 1562

Answers (2)

Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10015

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);

Upvotes: 0

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

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);
}

Upvotes: 2

Related Questions