2

In other words, is there a SPField.ListsFieldUsedIn() equivalent in SharePoint 2007? Ref: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.listsfieldusedin.aspx

1
  • I checked both SPField and SPList class members. Nothing in either have any property or method that will give me a connection between the two. Please help. I need to findout whether a given Site Column is already added to the List.
    – Tom
    Mar 8, 2012 at 16:56

1 Answer 1

1

Here's how it's done in SharePoint 2010:

public ICollection<SPFieldTemplateUsage> ListsFieldUsedIn()
{
    List<SPFieldTemplateUsage> list = new List<SPFieldTemplateUsage>();
    using (SPSqlCommand command = new SPSqlCommand())
    {
        using (SqlDataReader reader = this.GetSqlDataForPushdown(command))
        {
            if (reader != null)
            {
                while (reader.Read())
                {
                    SPFieldTemplateUsage item = new SPFieldTemplateUsage();
                    item.WebID = (Guid) reader[0];
                    item.ListID = (Guid) reader[1];
                    list.Add(item);
                }
                reader.Close();
            }
            return list;
        }
    }
}

private SqlDataReader GetSqlDataForPushdown(SPSqlCommand cmd)
{
    SPSite site = this.Fields.Web.Site;
    SqlSession sqlSession = site.SqlSession;
    cmd.SetStoredProcedure("proc_ListsUsingFieldTemplate");
    cmd.Parameters.AddWithValue("@SiteId", site.ID);
    cmd.Parameters.AddWithValue("@FieldId", this.Id.ToByteArray());
    cmd.Parameters.AddWithValue("@BaseTypes", SPBaseFieldId.BaseTypesFromFieldId(this.Id));
    return sqlSession.ExecuteReader((SqlCommand) cmd);
}

I don't have a SharePoint 2007 VM handy to see if the stored proc exists.

The alternative is to loop through all the lists in the site collection and check the Fields collection. It won't be fast but it'll do the trick.

1
  • Thanks for the code Rob. If you could post the loop, I could mark it as answer. In fact, that is exactly what I ended up doing. I had to loop through all the lists in the site collection and use the List.Fields() method.
    – Tom
    Mar 9, 2012 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.