Thursday, May 16, 2013

Running LINQPad as different user



Often you will need to impersonate LINQPad to perform some scripts in the context of other user (permissions etc).
LINQPad has no function of its own so what I do is use this simple command in a bat file:

runas /profile /user:domain\spAppPool "c:\Tools\LinqPad\2.0\LINQPad.exe"

Optionally you can right click on LINQPad exe while holding shift to "Run as different user..."


Tuesday, April 30, 2013

Invalid URI: The Uri string is too long with XmlDocument

When you get:
Invalid URI: The Uri string is too long

on XmlDocument while loading xml from string make sure you use:
XmlDocument.LoadXml(string)
"Loads the XML document from the specified string."

 and not
XmlDocument.Load(string)
"Loads the XML document from the specified URL."

Thursday, April 25, 2013

How to force web, whole list or library to be re-crawled by search in SharePoint 2013 using API

There is a new option in SharePoint 2013 that allows you to reindex list or document library.
You can find more about this on those blogs: 

Important: It does not work in SharePoint 2010!

It is also possible to do it for whole web. Thanks to this all items in all lists will be re-crawled.

I would like to show you how to do it using API.
When using API you can use it on any type of list. It also works for non-document library items, like tasks.

Re-crawl Web


void Main()
{
 using (SPSite site = new SPSite("http://demosvr1/web1/"))
 using (SPWeb web = site.OpenWeb())
 {
  object versionObj = web.AllProperties["vti_searchversion"];
  int version = ((versionObj is int) ? ((int) versionObj) : 0); 
  
  web.SetProperty("vti_searchversion", (version + 1));
  web.Update();    
 }
}


Re-crawl list


void Main()
{
 using (SPSite site = new SPSite("http://demosvr1/web1/"))
 using (SPWeb web = site.OpenWeb())
 {    
  SPList docList = web.Lists["Documents"];
  object versionObj = docList.RootFolder.Properties["vti_searchversion"];
  int version = ((versionObj is int) ? ((int) versionObj) : 0); 
   
  docList.RootFolder.SetProperty("vti_searchversion", (version + 1));
  docList.Update();
 }
}

Tuesday, April 23, 2013

LINQPad: Unable to contact licensing server

Good news for those who get Unable to contact licensing server while activating LINQPad or are on some private network with no public internet access - there is a way to activate it 'offline'.

  1. Try to activate program - wait for error message.

    Unable to contact licensing server

    Now LINQPad has put an extended activation code into your clipboard.

  2. Go to LINQPad offline activation page and generate offline activation code.

  3. Go to activation window in LINQPad

    activation window in LINQPad
  4. Right click on "Activation requires Internet connectivity."

    Right click on Activation requires Internet connectivity.
  5. You will get "Offline activation" window

    "Offline activation" window
  6. Paste your offline activation code, click "Activate"

  7. And you are done!

    Activation sucessfull

Thursday, March 21, 2013

Using LINQPad with SharePoint 2013 - BadImageFormatException and PlatformNotSupportedException


If you get any of those errors when running against SharePoint API in LINQPad:



BadImageFormatException: Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.


PlatformNotSupportedException: Microsoft SharePoint is not supported in 32-bit process. Please verify that you are running in a 64-bit executable.


You need to get yourself an AnyCPU version of LINQPad. Good thing that the author made one available.
It's called Download X64 build