Authenticating SharePoint 2010 and 2013 using Rest API.
SharePoint supports various Authentication mechanism including the Federated Login, legacy authentication model like Kerbos, Cliams based.If someone who worked on integrating the third party Apps using Oauth 2.0 or Oauth 1.0, definately they are not going to get impressed with the SharePoint Authentication , including me. I have integrated my apps with public storages like Google Drive, Dropbox, Box .,including hosted storages like ECM's especially Alfresco, Documentum 7.1 , CMIS.
Lets get into the SharePoint 2010 & 2013 integrating with Java Client. We will take example with PasswordAuthenticaion from java.net package and connect the sharepoint url by passing the UserName and Password. This is like a BasicAuthentication.
public class SharepointAuthenticator extends Authenticator{
private Properties properties;
public SharepointAuthenticator(Properties props){
properties = props;
}
public PasswordAuthentication getPasswordAuthentication () {
return new java.net.PasswordAuthentication (properties.getProperty("username"),properties.getProperty("password").toCharArray());
}
}
Thats it. its Simple BasicAuthentication.
Next step, how to connect to any of the Sharepoint Rest Api. In our example, we will take SharePoint List and SharePoint delete functions.
Integrating the Java Sharepoint Rest Api:
Lets put the sharepoint contextual urls in a property file say sharepoint.properties.Create a SharePoint Context bean say SPContext which can hold the account
Sharepoint.properties
copy.endpoint=/_vti_bin/Copy.asmx
list.endpoint=/_vti_bin/Lists.asmx
public class SPContext
{
private String uri;
private String domain;
private String username;
private String password;
private String listName;
public SPContext(String uri,String domain,String username,String password,String listName)
{
this.uri = uri;
this.domain = domain;
this.username = username;
this.password = password;
this.listName = listName;
}
// Getters & Setters
}
Java SharePoint List and SharePoint Delete:
For getting the sharepoint list, i have provided the sample code based that works the xml based request and response.
Any query while filtering the list is configurable in SharePointBase. Also it has createFile and Download file from the Sharepoint which canbe helpful.
//Call the list of Files & Folders.
//sharepoint url, domain name , username, password and listname which is related to sharepoint name
SPContext context=new SPContext( uri, domain, username, password, listName);
java.util.Properties prop = new java.util.Properties();
prop.load(getClass().getResourceAsStream("/SharePoint.properties"));
prop.put("baseurl", context.getUri());
prop.put("username", context.getDomain() + "\\" + context.getUsername());
prop.put("password", context.getPassword());
SharePointBase base = new SharePointBase(prop);
return base.getAllFiles(context.getListName(), folderId);
For SharePointBase and XmlUtils. Please check the attachment
Download: SharePointBase , XmlUtils
For SharePoint Delete using Rest API , here is the snippet.
java.util.Properties prop = new java.util.Properties();
prop.load(getClass().getResourceAsStream("/SharePoint.properties"));
prop.put("baseurl", context.getUri());
prop.put("username", context.getDomain() + "\\" + context.getUsername());
prop.put("password", context.getPassword());
SharePointDeleteListItem example = new SharePointDeleteListItem(prop);
int index = resourceId.lastIndexOf('/');
String name = resourceId;
String path = "";
if (index != -1)
{
name = resourceId.substring(index + 1, resourceId.length());
path = resourceId.substring(0, index);
}
example.executeQueryAndPerform(context.getListName(), context.getUri(), name, "", "Delete",resourceId);
Download: SharePointDeleteListItem
GoodLuck with your Integration.
No comments:
Post a Comment