3/14/2009

SharePoint: Finding SharePoint GUIDs

 

Update… I put together a PowerShell version of this here: http://techtrainingnotes.blogspot.com/2011/06/finding-sharepoint-guids-using.html  (so now you have four versions!)

Update… I put together a version of this that uses the SharePoint web services so you can get the GUIDs without having to be on the server. The EXE and the C# project can be downloaded here.

So now you have three versions, API from a Windows app, API from a LAYOUTs page and Web Services from a Windows app.

---

 

 

Just a little code to share….   :-)

 

I needed a quick way to find the GUIDs used on a SharePoint site so I wrote a little C# routine to display them. Below is the sample code for both a Windows app and a SharePoint Layouts ASPX page.

 

The Windows Version:

As this calls the API instead of the web services so this will need to be run from one of the SharePoint servers.

Add a textbox, a button and a listbox on a form then…

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {  InitializeComponent();    }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = listBox1.SelectedItem.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            SPSite site;
            try
            {
                site = new SPSite(txtSiteURL.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            listBox1.Items.Add("Site: " + site.ID.ToString());
            SPWeb web = SPContext.Current.Web;  //site.RootWeb;
            listBox1.Items.Add("Web: " + web.ID.ToString());
            foreach (SPList list in web.Lists)
            {
                listBox1.Items.Add(list.Title + ": " + list.ID.ToString());
            }

        }

    }
}

 

The Layouts folder version:

Create a text file in the SharePoint LAYOUTS folder with a name like “getGuids.aspx” and paste the following code. Run the page from any site:  http://youserver/yoursite/_layouts/getGuids.aspx

<%@ Page Language="C#"  %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    private void Page_Load(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        SPSite site;
        site = SPContext.Current.Site;
        ListBox1.Items.Add("Site: " + site.ID.ToString());
        SPWeb web = SPContext.Current.Web;   // site.RootWeb;
        ListBox1.Items.Add("Web: " + web.ID.ToString());
        foreach (SPList list in web.Lists)
        {
            ListBox1.Items.Add(list.Title + ": " + list.ID.ToString());
        }
    }

    private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = Request.Form["ListBox1"];
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="530px" />
        <br /><br />
        Click to copy to the text box.<br />
        <asp:ListBox ID="ListBox1" runat="server"
          OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
          Rows="20" AutoPostBack="True"/></div>
    </form>
</body>
</html>

7 comments:

Lon said...

I am using the layouts folder version.
Thanks,
Lon

Anonymous said...

I used the app. Super simple. Thanks a million.

Janus0104 said...

Hmm... worked before, but now I'm getting errors all the time.
"Client found response content type of 'text/html; charset=utf-8', but expected 'text/html'. The request failed with the error message: -- HTML Source of the page that I entered".

The version for _layouts folder doesn't work for me since SPContext.Current.Site is the SiteCollection no matter how deep I'd browse.

Mike Smith said...

Janus0104,

> The version for _layouts folder doesn't work for me since SPContext.Current.Site is the SiteCollection no matter how deep I'd browse.

Good catch! And an easy fix...

Change:
SPWeb web = site.RootWeb;

to:
SPWeb web = SPContext.Current.Web;

(I've updated the code in the article with this change.)


> Client found response content type of 'text/html; charset=utf-8', but expected 'text/html'

That must be from the downloadable copy with the web service call as that error is typically displayed when making a web service call and that call timed out or errored and returned an HTML error message. This could be a permissions issue between your local PC login and SharePoint.

Mike

Mike Smith said...

I have posted a PowerShell version of the above here: http://techtrainingnotes.blogspot.com/2011/06/finding-sharepoint-guids-using.html

Mike

Janus0104 said...

Tool does work, issue was between keyboard and chair :)

Confirming issue with layouts version being fixed by the correct call to SPContext.Current.Web

Thanks for the timely responses.

Sveinung said...

Hi,
Very interesting and usefull I belive, but I'm using Sharepoint 2010 online (Office 365 Enterprise) and I'm struggeling to get any of your alternative solutions for retriving the Site GUID in this environment. Any suggestions?

Thanks,
Sveinung

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.