Tuesday, January 17, 2012

Search SharePoint column in whole site

Sometimes we require to search a column ( for example Taxonomy or any other custom column) with in the whole site collection. Here is the sample code to search.


using (SPSite site = new SPSite("{your_site}"))
{
using (SPWeb web = site.OpenWeb())
{
SPField field = web.Fields.GetFieldByInternalName("Taxonomy");//or any other column

Console.WriteLine("Field: {0}", field.InternalName);
Console.WriteLine("==============================");

var usage = SPSiteColumnUsage.GetUsages(field);
foreach (var u in usage)
{
Console.WriteLine("Id: {0}", u.Id);
Console.WriteLine("Scope: {0}", u.Scope);
Console.WriteLine("ContentTypeId: {0}", u.ContentTypeId);
Console.WriteLine("IsUrlToList: {0}", u.IsUrlToList);
Console.WriteLine("ContentType or Url: {0}", u.Url);
Console.WriteLine();
}
Console.ReadLine();
}
}


SPSiteColumnUsage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;


namespace ConsoleApplication1
{
    class SPSiteColumnUsage
    {
        public static IList<SPSiteColumnUsage> GetUsages(SPField field)
        {
            List<SPSiteColumnUsage> list = new List<SPSiteColumnUsage>();

            if (field != null && field.UsedInWebContentTypes)
            {
                // Use reflection to get the fields collection for the specified field
                SPFieldCollection fieldCollection = field.GetType().GetProperty("Fields", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(field, null) as SPFieldCollection;
                if (fieldCollection != null)
                {
                    // Get the web context from the collection
                    SPWeb web = fieldCollection.Web;

                    // First collect all contenttypes to an array, so we can use Linq
                    var contentTypes = web.ContentTypes.Cast<SPContentType>();

                    // Filter contenttypes where field is used
                    var contentTypesWithField = (from contentType in contentTypes
                                                 where contentType.Fields.ContainsField
(field.InternalName)
                                                 select contentType).ToArray();

                    // Create usages for fields in contenttypes
                    foreach (SPContentType contentType in contentTypesWithField)
                    {
                        SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.InternalName, field.Scope, contentType.Name);
                        list.Add(siteColumnUsage);
                    }

                    // Get unique lists-urls where contentypes are beeing used
                    var listUrlsContentTypeUsage = (from contentType in contentTypesWithField
                                                    let contentTypeUsages = SPContentTypeUsage.GetUsages(contentType)
                                                    from contentTypeUsage in contentTypeUsages
                                                    where contentTypeUsage.IsUrlToList
                                                    select contentTypeUsage.Url).Distinct().ToArray();

                    // Create usages for fields in list
                    foreach (string listUrl in listUrlsContentTypeUsage)
                    {
                        SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.Id.ToString(), field.Scope, listUrl);
                        list.Add(siteColumnUsage);
                    }
                }
            }

            return list;
        }

        //private readonly Guid _Id;
        private readonly string _Id;
        private readonly string _Scope;
        private readonly SPContentTypeId _ContentTypeId;
        private readonly string _Url;

        private SPSiteColumnUsage(string id, string scope, SPContentTypeId contentTypeId)
        {
            _Id = id;
            _Scope = scope;
            _ContentTypeId = contentTypeId;
            _Url = null;
        }

        private SPSiteColumnUsage(string id, string scope, string url)
        {
            _Id = id;
            _Scope = scope;
            _ContentTypeId = SPContentTypeId.Empty;
            _Url = url;
        }

        public string Id
        {
            get { return _Id; }
        }

        public string Scope
        {
            get { return _Scope; }
        }

        public SPContentTypeId ContentTypeId
        {
            get { return _ContentTypeId; }
        }

        public string Url
        {
            get { return _Url; }
        }

        public bool IsUrlToList
        {
            get { return _ContentTypeId.Equals(SPContentTypeId.Empty) && !string.IsNullOrEmpty(_Url); }
        }
    }
}

Getting SharePoint list items in javascript

We can get the Sharepoint list items with in javascript code by using the jQuery SPServices plug in. Here is the code snippet.

<script src="{path-to}/jquery.min.js" type="text/javascript"></script>
<script src="{path-to}/jquery.SPServices-0.5.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
var queryStringValues = $().SPServices.SPGetQueryString();
var id = queryStringValues["ID"];
   
$(document).ready(function() {
var query = "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Text'>" + id + "</Value></Eq></Where></Query>";
var url = window.location;
$().SPServices({
operation: "GetListItems",
listName: "ListNameHere",
async: false,
CAMLQuery: query,
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName=z:row]").each(function(){
id = $(this).attr("ows_ID");
url = $().SPServices.SPGetCurrentSite() + "/Lists/ListName/DispForm.aspx?ID=" + id;
});
}
});
});
</script>

Have Fun.