Coding against K2’s SharePoint Services

When we want to manipulate SharePoint objects from within K2, dropping into code is sometimes the best option.

K2 uses its own web services to work with SharePoint and these are the ones actually used by its SharePoint event wizards.

We can also use them to get things done to our requirements. I have used them with great success over few years now.

The following server event code guarantees to update meta data fields of any list item (document, docset, list item, etc)

As long as the K2 service account has site collection admin rights this integration will work. The update will be done as part of the K2 workflow.

Using the run as option on the server event (recent feature addition) you can make this update to happen as another user identity – just make sure that identity has the correct rights.


using System;

using System.ComponentModel;

using System.ComponentModel.Design;

using System.Collections;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Drawing;

using SourceCode.KO;

using System.Net;

using System.Xml;

using System.Web;

using SourceCode.SharePoint.WebServices;

using SourceCode.Workflow.Common.Extenders;

using SourceCode.Workflow.Common.SharePoint;

using SourceCode.Workflow.Common.SharePoint.ProcessFields;

using SourceCode.Workflow.Common.Authentication;

using SourceCode.Workflow.Common.SharePoint.ListItem;

using SourceCode.Workflow.Common;

using SourceCode.Workflow.Common.Utilities;

using hostContext = Project_1b5f7d17eff546d5b3b07f55f54494b3.EventItemContext_f5e729db160d44dfa511046b95d521a9;

namespace ExtenderProject_1b5f7d17eff546d5b3b07f55f54494b3

{

public partial class EventItem_f5e729db160d44dfa511046b95d521a9 : ICodeExtender<hostContext>

{

public string getValue(string nameOfFied, K2SPField[] fs)

{

foreach (K2SPField f in fs)

{

if (f.InternalName.ToLower().Contains(nameOfFied.ToLower()))

{

return f.Value;

}

}

return string.Empty;

}

public bool isThisMyFile(string fileName, K2SPField[] fs)

{

foreach (K2SPField f in fs)

{

if (f.InternalName.ToLower().Contains(“fileleafref”))

{

if (f.Value.ToLower().Equals(fileName.ToLower()))

{

return true;

}

}

}

return false;

}

public void updateFileName(string newName, K2SPField[] fs, string docLibName, Object id, K2SPListItems _k2SPListItems )

{

setValue(“FileLeafRef”, newName, fs, docLibName, id, _k2SPListItems);

}

public void setValue(string FieldName,string newValue, K2SPField[] fs, string docLibName, Object id, K2SPListItems _k2SPListItems)

{

foreach (K2SPField f in fs)

{

if (f.InternalName.ToLower().Contains(FieldName.ToLower()))

{

f.Value = newValue;

_k2SPListItems.UpdateItem(docLibName, id, fs);

return;

}

}

}

public void updateDocumentMeta( Project_1b5f7d17eff546d5b3b07f55f54494b3.EventItemContext_f5e729db160d44dfa511046b95d521a9 K2,string siteURL, string docLibName, string filename)

{

ProxyCertification _proxyCertification = new ProxyCertification(K2.StringTable[“SmartObject Server”]);

K2SPListItems _k2SPListItems = new K2SPListItems();

ADCredentials _adCredentials = new ADCredentials();

K2SPListItem[] spItems;

_k2SPListItems.Url = General.FormatSiteURL(siteURL) + “/_vti_bin/K2SPListItems.asmx”;

_proxyCertification.GetCertificateForSsl(_k2SPListItems);

_k2SPListItems.Credentials = _adCredentials.GetCredentials(siteURL);

spItems = _k2SPListItems.GetListItemsFromName(docLibName);

foreach (K2SPListItem item in spItems)

{

//use if(item.Title….. check as required

K2SPField[] fs = _k2SPListItems.GetItemDetailsFromListNameItemId(docLibName, item.ID, false);

if (isThisMyFile(filename, fs))

{

string revision = getValue(“Revision”,fs);

updateFileName(“newName ā€œ + revision, fs,docLibName,item.ID,_k2SPListItems);
return;

}

}

}

public void Main(Project_1b5f7d17eff546d5b3b07f55f54494b3.EventItemContext_f5e729db160d44dfa511046b95d521a9 K2)

{

string siteURL = “http://jeylabsdemo/test123&#8221;;

string listName = “Documents”;

updateDocumentMeta(K2, siteURL,listName,”jeyTest.xls”);

}

}

}

%d bloggers like this: