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>

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.