Monthly Archives: August 2013

Create Cross-site Lookup column in SharePoint

Recently there is new requirement from client to add lookup column to a list in the other site. In order to do this, one of the solution is to add site column in root site, so it can be used by all its sub sites.

If we create a lookup site column from root site, the source list in sub site is not available from “Get information from” section in column settings.

A quick solution is to create a lookup site column and manage code to link the column with list in sub site when feature activated.

Below is my Site column Elements.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Field Type="Lookup"
      DisplayName="ClientNo"
      StaticName="ClientNo"
      Name="ClientNo"
      Required="TRUE"
      EnforceUniqueValues="FALSE"
      List="Lists/Client"
      ShowField="Title"
      UnlimitedLengthInDocumentLibrary="FALSE"
      RelationshipDeleteBehavior="None"
      ID="{468A862E-183B-4F5C-A59F-8BA608117C09}"
      RowOrdinal="0"/>
</Elements>

In your Feature Receiver code:

public static class SPFieldLookupExtensions
{
   public static void UpdateLookupReferences(this SPFieldLookup lookupField, SPWeb web, SPList list, string sFieldName)
   {
      if (string.IsNullOrEmpty(lookupField.LookupList))
      {
         lookupField.LookupWebId = web.ID;
         lookupField.LookupList = list.ID.ToString();
         lookupField.LookupField = sFieldName;
         lookupField.AllowMultipleValues = true;
         lookupField.UnlimitedLengthInDocumentLibrary = true;
      }
      else
      {
         lookupField.SchemaXml = lookupField.SchemaXml.Replace("Lists/Client", list.ID.ToString());
         lookupField.LookupWebId = web.ID;
         lookupField.LookupField = sFieldName;
         lookupField.AllowMultipleValues = true;
         lookupField.UnlimitedLengthInDocumentLibrary = true;
      }
      
      lookupField.Update(true);
   }
}

[Guid("76335e5e-5365-4d4d-a1a2-e09c9d059fc5")]
public class CustomFeatureEventReceiver : SPFeatureReceiver
{
   public override void FeatureActivated(SPFeatureReceiverProperties properties)
   {
      try
      {
         // Get root site and web that list exists
         SPSite site = properties.Feature.Parent as SPSite;
         using (SPWeb web = site.OpenWeb(new Guid("{24a0bf4c-1234-4e78-5678-ebb42afb46e1}")))
         {
            if (web.Lists["Client"] != null && site.RootWeb.Fields["ClientNo"] != null)
            {
               SPList listClient = web.Lists["Client"];
               SPFieldLookup field = (SPFieldLookup)site.RootWeb.Fields["ClientNo"];
               field.UpdateLookupReferences(web, listClient, "ClientNameAndNumber");
            }
         }
      }
      catch (Exception ex)
      {
          // Error handler
      }
   }
}

This will do the work and hopefully it helps you.
But then, there is a more sexier solution that I found recently and you could use it for FREE.
DAMN … he is darn good.

Check this out: http://www.codeproject.com/Articles/619118/Cross-site-lookup-column-for-SharePoint-2013-and-S

Advertisements

My Experience in Provisioning Search Service Application in SharePoint 2013

Everyone has their own experience in Provisioning Search Service Application in SharePoint 2013. In my case, its not a smooth one but its not as bad as Provisioning User Profile Sync in SharePoint 2010.

I tried to gather all resources that I found in google which help me out in this article. Also, other articles that might help you out.

1. Apply patches to application and web servers as per suggestion below is a MUST.

http://social.technet.microsoft.com/Forums/sharepoint/en-US/50acc2b8-dd56-4d5a-a660-dffa325ef807/sharepoint-2013-search-not-provisioning-correctly

Microsoft even updated their Server preparation documents.

http://technet.microsoft.com/en-us/library/ee805948(v=office.15)#PrepareServers

 

2. It might be not a good / recommended practice to add “sysadmin” SQL role to your service account, but this is the easiest way to make it work.

http://www.uccorner.com/382/sharepoint/sharepoint-2013/will-search-be-the-new-user-profile-service-in-sharepoint-2013/

 

3. If your search topology is not displaying a green checks and some error message like “Unable to retrieve topology component health states. This may be because the admin component is not up and running“. You might want to manually re-create the Search Topology and associate Search Service Application to your new Search Topology.

http://social.technet.microsoft.com/Forums/sharepoint/en-US/1a4a3be6-bd9e-4474-9b0f-b8b8ca468e70/new-search-topology-stuck-on-activating

 

4. If your Search Host Controller Service stuck in “Starting” state.

http://mmman.itgroove.net/2012/12/search-host-controller-service-in-starting-state-sharepoint-2013-8/

 

5. Some people face the issue “Could not find or connect to hostcontroller on server SERVER-01. Exception: Could not connect to net.tcp://SERVER-01/ceres/hostcontroller/nettcp. The connection attempt lasted for a time span of 00:00:21.0369864. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond“.

http://distefano.biz/blog/index.php/2013/02/07/sp-2013-activate-search-topology-fails/

 

6. Best Practice way to Provision Search Service Application which utilize search, service, content access account.

http://technet.microsoft.com/en-us/library/gg502597.aspx

http://technet.microsoft.com/en-us/library/cc678863(v=office.15).aspx#Section3

http://blog.thewulph.com/?p=374

 

7. Remember my earlier post to limit resources to noderunner.exe process, DON’T DO IT in production or if you want to make your Search Service Application works.

http://leonzandman.com/2012/11/08/return-of-the-search-application-topology-component-health-state-error/

 

8. There are some powershell scripts to provision Search Service Application in SharePoint 2013. But the one that I used, tested and working found here.

http://melcher.it/2012/07/sharepoint-2013-create-a-search-service-application-and-topology-with-powershell/

Enjoy your reading and Hopefully it save you a lot of time…

Add Calendar List View WebPart to SharePoint page

Follow up from my previous article to Add List View WebPart to SharePoint Page, I spent a bit of time figuring out why setting Calendar view to the list view webpart is not working.

My custom list have two views (default list view & calendar view) and I would like to display the Calendar view when added to SharePoint page. After spending sometime trying to figure out, I finally find the solution. Hopefully it save you a lot of time. 

Solution

Change XsltListViewWebPart into ListViewWebPart. What the …. 

private void AddCalendarViewWebPart(SPFile file, SPList list, string webPartZone, int webPartOrder, Guid viewId)
{
   SPLimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
   ListViewWebPart wp = null;
   list.ParentWeb.AllowUnsafeUpdates = true;

   try
   {
       // create an instance of the ListViewWebPart
       wp = new ListViewWebPart();

       // convert the list GUID to a string, must include braces (ToString("B")) and be in uppers (ToUpper())
       wp.ListName = list.ID.ToString("B").ToUpper();

       // optionally set the title of the web part
       wp.Title = list.Title;
       wp.ViewGuid = viewId.ToString("B").ToUpper(); 
       wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

       // add the web part to the limited web part manager.  
       limitedWebPartManager.AddWebPart(wp, webPartZone, webPartOrder);

       // you need to update the list because a view was just added to it
       list.Update();
   }
   finally
   {
       // do some fun cleanup of disposable items.  if you are wondering about the .Web.Dispose() bit look for my
       // blog article on the memory leaks in the SPLimitedWebPartManager.
       if (limitedWebPartManager != null)
       {
           if (limitedWebPartManager.Web != null)
           {
              limitedWebPartManager.Web.Dispose();
           }

           limitedWebPartManager.Dispose();
       }

       if (wp != null)
       {
           wp.Dispose();
       }
       list.ParentWeb.AllowUnsafeUpdates = true;
   }
}

Add List View WebPart to SharePoint page

The way we add custom WebPart to a SharePoint page is by adding <AllUsersWebPart> element with WebPart’s exported xml populated inside <File> element. I will NOT discuss it further in here.

As you would have known, List View WebPart could NOT be exported in SharePoint page. Therefore, the question is How could we add the List View WebPart into SharePoint page?

I did it in managed code when Feature Activated feature receiver.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   try
   {
      SPWeb web = properties.Feature.Parent as SPWeb;

      // Add webparts into MyView.aspx page
      SPFile myviewFile = web.GetFile("Pages/myview.aspx");
      AddListViewWebPart(myviewFile, web.Lists["ListName"], "Top", 1, web.Lists["ListName"].Views["ViewName"].ID);
   }
   catch (Exception ex)
   {
      // Your Error handling here ...
   }
}

private void AddListViewWebPart(SPFile file, SPList list, string webPartZone, int webPartOrder, Guid viewId)
{
   SPLimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
   XsltListViewWebPart wp = null;
   list.ParentWeb.AllowUnsafeUpdates = true;

   try
   {
       // create an instance of the ListViewWebPart
       wp = new XsltListViewWebPart();

       // convert the list GUID to a string, must include braces (ToString("B")) and be in uppers (ToUpper())
       wp.ListName = list.ID.ToString("B").ToUpper();

       // optionally set the title of the web part
       wp.Title = list.Title;
       wp.ViewGuid = viewId.ToString("B").ToUpper();
       wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

       // add the web part to the limited web part manager.
       limitedWebPartManager.AddWebPart(wp, webPartZone, webPartOrder);

       // you need to update the list because a view was just added to it
       list.Update();
   }
   finally
   {
       // do some fun cleanup of disposable items.
       if (limitedWebPartManager != null)
       {
           if (limitedWebPartManager.Web != null)
           {
              limitedWebPartManager.Web.Dispose();
           }

           limitedWebPartManager.Dispose();
       }

       if (wp != null)
       {
           wp.Dispose();
       }
       list.ParentWeb.AllowUnsafeUpdates = true;
   }
}

If you are planning to set Calendar view for the list, this will not work. Take a look on my other article to Add Calendar List View WebPart to Page.

%d bloggers like this: