Monday, September 7, 2015

The query cannot be run for the following DataObject: GetCommonManager

I got that message when I previewed an InfoPath form (SharePoint Server 2010, InfoPath 2010):

The query cannot be run for the following DataObject: GetCommonManager 
InfoPath cannot run the specified query. 
The SOAP response indicates that an error occurred on the server: 
Server was unable to process request. ---> Object reference not set to an instance of an object.

I knew that new User Profile Service Application had been created and it used to work with old one without issues. The UserProfileService web service was healthy and calls for GetUserProfileByName were proceeded well. It just stuck with GetCommonManager call.

After some investigation I found My Site host was not defined in that brand new User Profile Service Application. When I defined it My Site Settings for the application  GetCommonManager started working again.

Hope that helps save time someone.

Accessing mixed authentication web app with Client Object Model and Web Serices

i have mixed authentication web app in SharePoint 2010: both Windows (NTLM) and Forms based.
When I tried to access it using Client Object Model with my NTLM credentials I got 401 (Unauthorized) exception. I googled and found this solution

...
clientContext.ExecutingWebRequest += new EventHandler(clientContext_ExecutingWebRequest);
...
}

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}


This works well for me.

Then when I tried to access same site using Lists.asmx web service with same NTLM credentials I got exactly same exception.
I added this method to the Web Service wrapper class (it's in Web References > ListWebService > Reference.map > Reference.cs in my case)


protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.WebRequest wr = base.GetWebRequest(uri);
wr.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
return wr;
}

That solved the issue as well.

Sunday, September 6, 2015

Download Files/Images from SharePoint Libraries using PowerShell

I want to get this job done hastily. So for a moment I thought of Power Shell. I want to give it a try and trust me by using this I was able to download gigs of documents within minute. Below is the code snippet for your reference:

######################## Start Variables ########################
######################## Download Files Script######################
$destination = $args[0]
$webUrl = $args[1]
##############################################################
# Replace actual document libraries name with DocLib1, 2, 3 etc.
$listUrls=”DocLib1″,”DocLib2″,”DocLib3″,”DocLib4″,”DocLib5″
$web = Get-SPWeb -Identity $webUrl
foreach($listUrl in $listUrls)
{
$list = $web.GetList($webUrl+”/”+$listUrl)
function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + “/” + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + “/” + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
        }
}
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}}

Copy this to a notepad file and save with extension .ps1 E.g.DownloadFiles.ps1
Open SharePoint Management Shell 2010; navigate to the path of script. Provide destination path as argument 1 and site URL as argument 2 as shown below.  
.\DownloadFiles.ps1 “d:\documents” http://mySite
*This will create separate folders for each document library with library name.

PeoplePicker not showing FBA users (Claims Based Authentication)

user in a site group. On searching the user in people picker, it was not showing the FBA user(s) to select. Every time i searched the FBA user(s) through people picker, it returned me only user(s) from AD. I searched Google but no clear instructions on how to resolve this issue. Finally after some R&D, i was able to resolve this. So i thought to pen it down step by step for you guys. Here are the steps.
First we need to know the values of ‘Membership Provider’ and ‘Role Manager’ of web application.
1. Navigate to Central Administration > Application Management > Manage web applications (Under Web Applications section)
Or
Directly navigate to http://<CA URL>/_admin/WebApplicationList.aspx
2. Select web application (That’s need to be configure for FBA users search.).
3. Click on Authentication Providers from ribbon.
4. Inside Authentication Providers pop up, Click on ‘Default’ hyperlink under zone column.
5. On Edit Authentication Modal popup page, go to Claims Authentication Types.
6. Note down the value of ‘ASP.NET Membership provider name’ and ‘ASP.NETRole manager name’.
7. Exit from the CA.
Make entries in web.config of application.
1. Open web.config file inside ‘C:\inetpub\wwwroot\wss\VirtualDirectories\<Application Folder>’ folder.
2. Search for ‘<PeoplePickerWildcards>’
3. Now add values of ‘Membership Provider’ and ‘Role Manager’ in this section as shown below.
<PeoplePickerWildcards>
<clear />
<add key=”<Your Membership Provider Value Goes Here>” value=”%” />
<add key=”<Your Role Manager Value Goes Here>” value=”%” />
</PeoplePickerWildcards>
4. Save and close the web.config.
5. Now find FBA user through people picker and it will work.

Finding features in a content database in SharePoint 2010 using PowerShell or tools

Sometimes we have a feature id and we want to know the places where it is active, even as a dummy one. Below mentioned script might be helpful.
http://get-spscripts.com/2011/06/removing-features-from-content-database.html
or
http://featureadmin.codeplex.com/downloads/get/290833
or
http://archive.msdn.microsoft.com/WssAnalyzeFeatures

Tuesday, February 25, 2014

.NET Framework 4.5 Zip facility (Zip compression)

ip is one of the most accepted archive file formats. Zip format is supported in almost all operating systems with some built-in name.
  • In Windows operating system it’s implemented by the name “Compressed folders”.
  • In MAC OS it’s implemented by the name “Archive utility”.
Now in .NET we did not have built-in support for implementing Zip compression. Many developers where using third party components like “DotnetZip”. In .NET 4.5, the Zip feature is baked in the framework itself, inside the namespaceSystem.IO.Compression.
The first step is you need to reference two namespaces:
  • System.IO.Compression.FileSystem
  • System.IO.Compression
The next step is to import the below two namespaces:

Code Block :
 
using System.IO.Compression; 
Namespace zip
{
Class zip
{
ZipFile.CreateFromDirectory(@"D:\data",@"D:\data.zip");
ZipFile.ExtractToDirectory(@"D:\data.zip", @"D:\data\unzip");
}
}

Java Generics Basics

his Java generics tutorial is to introduce basic terminologies related to generics. They are demonstrated with example code and this tutorial is for beginners only.

Java Generics Example

Before we go into detail let us see an example for generics in Java. This is to recall and continue from our previous tutorial for introduction to generics. In the following example we are using the generics type implemented for collections. This shows how we can use a generic type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class JavaGenericsExample {
  public static void main(String args[]) {
    List<String> animalsList = new ArrayList<String>();
    animalsList.add("Lion");
    animalsList.add("Crocodile");
    animalsList.add("Hyena");
    for (String animal : animalsList) {
      System.out.println(animal);
    }
  }
}

Generic Type

A generic type is the definition of a generic class. The generic type definition will have a placeholder for a type. Following are examples from Java JDK API for generic types

List – JDK Source

public interface List<E> extends Collection<E> {
...
 boolean add(E e);
...
}

ArrayList – JDK Source

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
...
}

Parameterized Type

When a generic type is instantiated, it is called parameterized type. We supply a class/type as parameter for placeholder and create a new object for a generic type. In the above java generics example, we have instantiated a List object by supplying the parameter argument as String.
List<String> animalsList = new ArrayList<String>();

Wildcard Type

Using a generic type with wildcards is wildcard type. In the below example, we have written a new method using generics which uses the wildcard type. We have not give any actual parameter as argument in the topTwo method. We have used a wildcard ? as argument. This states that we can call this method by passing a List containing any type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class GenericsWildcardType {
  public static void main(String args[]) {
    List<String> animalsList = new ArrayList<String>();
    animalsList.add("Lion");
    animalsList.add("Crocodile");
    animalsList.add("Hyena");
    List<?> topTwoAnimals = topTwo(animalsList);
    System.out.println(topTwoAnimals);
  }
  public static List<?> topTwo(List<?> list) {
    return list.subList(0, 2);
  }
}

Bounded Wildcard Type

We can restrict the wildcard type arguments as applicable to only a specific subtype or supertype. Instead of any type at the place of the wildcard ?, it is restricted to the sub or super classes of a specific class. For example, in the above wildcard type example we can add the keyword ‘extends’ as below and restrict it to a subtype only. This is known as upper bound. Similarly ‘super’ can be used to restrict it to a supertype and is known as lower bound.
public static List<?> topTwo(List<? extends String> list) {
  return list.subList(0, 2);
}

Generics Custom Type

Following is an example for Java generics demonstrating generic type, parameterized type, wildcard type and bounded wildcard type.
package com.javapapers.java;
/*
 *OUTPUT:
 Java Generics Rocks: Tea
 Java Generics Poured: Tea
 */
public class CustomGenericType {
  public static void main(String args[]) {
    Cup<Drink> cup = new Cup<Drink>();
    Cup<Tea> cupOfTea = new Cup<Tea>(new Tea());
    System.out.println("Java Generics Rocks: " + cupOfTea.pour());
    cup.fillFrom(cupOfTea);
    System.out.println("Java Generics Poured: " + cup.pour());
  }
}
class Drink {
  @Override
  public String toString() {
    return "empty";
  }
}
class Tea extends Drink {
  @Override
  public String toString() {
    return "Tea";
  }
}
class Cup<E> {
  String content;
  public Cup() {
  }
  public Cup(E e) {
    content = e.toString();
  }
  public void fillFrom(Cup<? extends Drink> c) {
    content = c.pour();
  }
  public String pour() {
    return content;
  }
}
This Java generics tutorial you have read now is part two of Generics series and there is lot more to come, keep watching.