Pages

Tuesday, January 3, 2012

How to set people and group column programmatically using Client Object Model


using SP = Microsoft.SharePoint.Client;
using (SP.ClientContext _ctx = new SP.ClientContext("http://YourSiteUrl"))
{
  SP.ListItem _listItem = null;

  ctx.Credentials = CredentialCache.DefaultCredentials;
 
  SP.List _testList = web.Lists.GetByTitle("TestList");
  SP.CamlQuery _query = new SP.CamlQuery();
  query.ViewXml = @"<View><Query><Where><Geq><FieldRef Name='ID'/>" + "<Value Type='Number'>10</Value></Geq></Where></Query></View>";
  SP.ListItemCollection _listitems = _configList.GetItems(_query);

  ctx.Load(_listitems);
  ctx.ExecuteQuery();
 
  if (_listitems != null && _listitems.Count > 0)
  {
     _listItem = _listitems[0];

     SP.FieldUserValue _userValue = new SP.FieldUserValue();
     SP.User _newUser = _ctx.Web.EnsureUser("domain\\username");
     _ctx.Load(_newUser);
     _ctx.ExecuteQuery();

     _userValue.LookupId = _newUser.Id;

     _listItem["userfield"] = _userValue;
     _listItem.Update();

     _ctx.ExecuteQuery();

  }   

);

Wednesday, December 28, 2011

How to hide properties in Document Information Panel – SharePoint

Document Information Panel – This is the window which shows all the properties associated with the document. Let it be out of the box / default properties (like Title, Keywords), or custom properties or content type properties.

Wait, what are content type properties?
Consider a scenario where you have a SharePoint site and a document library. You create some site columns and then create content type for your document library. (Inheriting from the document content type). Now you associate this newly created content type to your document library and done.

You go ahead and click on create New Document link and Word Editor opens up. Now there you see the Document Information Panel (DIP) containing all the site columns in your content type. These are metadata holders for your document. And that’s why these document properties are called as content type properties.

In Following Image, I have opened the document from the SharePoint document library and added Version and Comments column in the default document content type , which are visible in DIP.


Issue:
There were some of the content type properties which were getting displayed in DIP, but I didn’t want users to see those /change those. In short I needed to hide those.
Let’s try to hide version property from DIP.
Solution:
First thought comes to mind like – ok these properties are nothing but the site columns right? So why don’t we just delete the site columns and get rid of properties in DIP? But NO. Because if we delete the site columns from the content type , the DIP won’t show the property but document will not be holding the metadata for that column.
So what could be done?
Trick I found is, just hide the site column.
How we can do it?
Library settings > Find your content type and click on it > select the site column which you don’t want to see in DIP > click on Hidden option in advanced option and select ok.



Now DIP will not show the content type property when creating new documents from the library
Version column gets hidden from DIP



Note: This approach is used only when the site column is not of type mandatory/required. As per my knowledge you cannot hide the column which is required.


How to debug the word add in project in Visual Studio

Last few days I was playing much with MS word add ins and its development, Yes you are right, when the word ‘develop’ comes into the picture , each and every developer needs to think some logic and then they let the figures do the remaining job. There are many times when run-time error occurs and developer go ahead and track/find them using debugger.
Most of us use Visual Studio, the developer’s heaven as IDE for the .NET based application development and Microsoft has given way too good features to debug your code. (Link here)
Some examples:
·         When debugging web based projects / SharePoint farm solutions , one attaches the debugger to the w3wp.exe (IIS process)
·         When debugging sandboxed solutions one needs to attach the debugger to the SPUCWorkerProcess.exe (SPUC)
·         And again while debugging the timer jobs in SharePoint debugger needs to be attached to Timer service. (OWSTimer.exe)
Ok ok, before I get the question like – come on we know all of these.. I will come to the point directly.
I learnt a lesson that if you need to debug the MS Office Addins then you must attach the debugger to the appropriate MS Office Client.
In my case, as I was working with MS Word Addin – so I needed to attach the debugger to WINWORD.exe service, like this

 
If you do not see WINWORD.exe process , make sure that you have launched the MS Word Client and selected – Show all process , show process from all users/sessions in the attach debugger window.
Also If the debugger is not getting attached, then make sure that the add-in is referring to the dll is latest in the respective location. (For example If add in is referring to the dll somewhere in release folder of your solution then make sure that the dll is latest)

Wednesday, November 16, 2011

How to add web part on SharePoint 2010 Wiki Pages / On Rich Content Programmatically


While working with SharePoint 2010 one of my fried came across a scenario where he needed to modify the home page of team site using a feature. Feature should delete the existing content in out of the box team site’s home page and add new web parts.

Though this sounds very simple in first shot but practically little tricky, that’s what I experienced.
SharePoint 2010 has come up with new features and one of them is modification in team sites. If you observe that when a new out of the box team site is created, there is no default.aspx instead default page is replaced by Home.aspx. Home.aspx is a wiki page and resides in the Site Pages library of team site.
As this is a wiki page so it’s good that this supports direct editing and you can add web parts directly in the rich text area using UI. But when it comes to adding web parts to this page programmatically then problem begins.

Waldek Mastykarz has given excellent workaround to this problem where he has shown how to add a web part in Publishing Page Content of publishing page.  We can use similar technique to add web parts to wiki page as well, only difference is of field name of wiki page.
In the example I have added two list view web parts in Home.aspx page , one reside at left and other one is at right of page. This can be done with simple HTML markup where <td> and <tr> are defined with certain widths.

In my case Ultimate output should be like this HTML
//<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\">
//  <table id=\"layouts table\" style=\"width:100%\">
//      <tr style=\"vertical-align: top;\">
//          <td style=\"width=66.6%\">
//              <div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div>
//          </td>
//          <td style=\"width=33.3%\">
//              <div class=\"ms-rtestate-read {1}\" id=\"div_{1}\"></div>
//          </td>
//      </tr>
//  </table>
//</div>

In example above {0} and {1} would be replaced by GUIDs of web parts.
I have tried this with console application for POC but one can add this in feature receiver.



using (SPSite site = newSPSite("http://yoursite/"))
{
using (SPWeb _web = site.OpenWeb("/yourteamsite"))
   {
SPList _list1 = _web.Lists.TryGetList("List1");
SPList _list2 = _web.Lists.TryGetList("List2");
SPList _sitePages = _web.Lists.TryGetList("Site Pages");

SPFile _file = _web.GetFile(_web.Url + "/SitePages/Home.aspx");

if (_list1 != null&& _list2 != null)
     {
XsltListViewWebPart _list1View = newXsltListViewWebPart();
       _list1View.ListId = _list1.ID;
       _list1View.ViewGuid = _list1.DefaultView.ID.ToString("B").ToUpper();
       _list1View.XmlDefinition = _list1.DefaultView.GetViewXml();

XsltListViewWebPart _list2View = newXsltListViewWebPart();
       _list2View.ListId = _list2.ID;
       _list2View.ViewGuid = _list2.DefaultView.ID.ToString("B").ToUpper();
       _list2View.XmlDefinition = _list2.DefaultView.GetViewXml();

if (_file != null)
       {
using (SPLimitedWebPartManager _mgr = _web.GetLimitedWebPartManager(_file.Url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
         {
Guid storageKey1 = Guid.NewGuid();
string wpId = String.Format("g_{0}", storageKey1.ToString().Replace('-', '_'));
          _list1View.ID = wpId;

Guid storageKey2 = Guid.NewGuid();
string _liWPID = String.Format("g_{0}", storageKey2.ToString().Replace('-', '_'));
           _list2View.ID = _liWPID;
           _list2View.ID = _liWPID;

//Deleting Web Parts
for (int i = _mgr.WebParts.Count - 1; i > 0; i--)
          {
            _mgr.DeleteWebPart(_mgr.WebParts[i]);
          }

//Adding Web Part
          _mgr.AddWebPart(_list1View, "wpz", 0);
          _mgr.AddWebPart(_list2View, "wpz", 1);

//Deleting Wiki Content
           _file.Item[SPBuiltInFieldId.WikiField] = string.Empty;
 _file.Item.UpdateOverwriteVersion();

string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><table id=\"layouts table\" style=\"width:100%\"><tr style=\"vertical-align: top;\"><td style=\"width:66.6%\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div></td><td style=\"width:33.3%\"><div class=\"ms-rtestate-read {1}\" id=\"div_{1}\"></div></td></tr></table></div>", storageKey1.ToString("D"), _ storageKey2.ToString("D"));


_file.Item[SPBuiltInFieldId.WikiField] = marker;
            _file.Item.UpdateOverwriteVersion();

Console.WriteLine("done");
          }
}

        }

       }
      }

Console.ReadLine();
   }

Working with SharePoint Multilingual Titles Programatically

Last week I was doing some R&D on SharePoint Multilingual user interface and hence thought to share some learning experience which I got.


Background:
SharePoint 2010 ships with the new feature called Multilingual User Interface, which is helpful for displaying the content in the web site in various languages depending upon user selection. I will not dive in to the much details of it as many of good posts are available on the same. Some examples can be found Here and Here.
Just a basic rule of this feature is, it works on the UI culture of the current thread, and shows content to user in different languages.

Problem:
So far so good, SharePoint 2010 manages to translate all link titles, list, libraries titles, site columns titles, content type’s titles and many more things which are out of the box. So when user selects a different language in MUI selector menu then he or she is able to see the OOB translated content very well , but what happens when we have our custom lists, list web parts, custom site columns, content types ? Those don’t get translated by default.

Solution:
One has said very well, there is always a solution J and this line cheers me up. So to overcome this kind of scenario there are two ways.

1.  Manual Approach: one has to go to each and every created custom lists, site columns, and content types and update titles of the same in different languages by changing languages every time using MUI selector. Though this sounds easy but takes lots of efforts when you have sites already provisioned and there is lots of such custom data.

2. Programmatic Approach: I always love such approaches and so I always try to find way to do them as they makes life easy by just few clicks.

A property “TitleResource” is available now with some SharePoint objects which is of type SPUserResourceclass, with which you can get or set the titles of webs, lists, site columns, content types for multiple language cultures.
Following example shows that how we can get or set the multilingual values of a site’s title for German UI culture.



using (SPSite site = newSPSite("http://YourSite"))
{
using (SPWeb _web = site.OpenWeb())
  {
Console.WriteLine(string.Format("{0}-{1}", "Title", _web.Title));

SPUserResource _userResource = _web.TitleResource;
if (_userResource != null)
    {
       _web.AllowUnsafeUpdates = true;
       _userResource.SetValueForUICulture(newSystem.Globalization.CultureInfo(1035), "German Title");
       _userResource.Update();
       _web.AllowUnsafeUpdates = false;

Console.WriteLine("{0}-{1}-{2}", "Updated Title for Culture", "German", _userResource.GetValueForUICulture(newSystem.Globalization.CultureInfo(1035)));

}
  }
}
Console.ReadLine();



Monday, October 31, 2011

Received Microsoft Community Contributor Award


Yesterday Night , I opened my emails and happen to see the Email From Microsoft mentioning that,

Dear Bhushan, 
Congratulations! We’re pleased to inform you that your contributions to Microsoft online technical communities have been recognized with the Microsoft Community Contributor Award. 

I am really thankful to Microsoft for recognizing my efforts and awarding me with such valuable award.

Here is the Certificate J


Saturday, October 1, 2011

SharePoint Localization in Sandbox Environment


Last week I was working with localization in SharePoint and just wanted to share some thoughts. We all know that this can be Managed with the Resource files and at run time we can read the values based on the culture (either UI culture or your site’s culture) and reosurce files are kept in the 14 hive of SharePoint Server file system.

I will not dive in to the details of the how to do localization in SharePoint but I can point out some references to do this , like this post of John Powell and also this MSDN post, explained in excellent way.

So far so good, but when we work with sandbox environment then we cannot access the file system of the server and so puzzle is where and how to keep this resource files?
Well but that didn’t take too much time to solve as MSDN has already given a very good documentation to take on this issue.
Here is link on how to do this : MSDN – Localization in Sandbox environment and this post which is a good pictorial explanation.

Some Quick Notes :

What are satellite assemblies? – Satellite assemblies are the compiled libraries which contains the localizable resources while creating multi lingual applications. So a satellite assembly per language / culture. – Reference Here

So where these satellite assemblies are placed in Sandbox solutions ? – when Sandbox solution with Satellite assembies is deployed then everything is packaged as wsp and deployed to site collection’s Solutions gallery. Reference Here