Posts Tagged list

SharePoint: Associate Workflow with a list programmatically

Scenario:

We need to associate a workflow with a list or create an instance of workflow for a list using SharePoint Object Model (OM)

Resolution:

In order to associate workflow with a list, we will be using CreateListAssociation method of SPWorkflowAssociaition class. We need following four values:

  • baseTemplate – The workflow template on which to base this workflow association.
  • Name – The name to give this workflow association.
  • taskList – The task list on which to create workflow tasks for this workflow association.
  • historyList – The list to which to log workflow history events

Code is as under:

using(SPSite spSite = new SPSite(“SiteURL”))

{

SPWeb spWeb = spSite.OpenWeb();

SPList assocList = spWeb.Lists[“NameOfTheList”];

SPWorkflowTemplate wflTemplate = spWeb.WorkflowTemplates.GetTemplateByName(“NameOfWorkflowTemplate”, System.Globalization.CultureInfo.CurrentCulture);

SPList wflTaskList = spWeb.Lists[“Tasks”];

SPList wflHistoryList = spWeb.Lists[“Workflow History”];

SPWorkflowAssociation wflAss = SPWorkflowAssociation.CreateListAssociation(wflTemplate, “NameOfTheWorkflow”, wflTaskList, wflHistoryList);

wflAss.AutoStartChange = false;

wflAss.AutoStartCreate = false;

wflAss.AllowManual = true;

spWeb.AllowUnsafeUpdates = true;

assocList.AddWorkflowAssociation(wflAss);

assocList.Update();

spWeb.AllowUnsafeUpdates = false;

}

Comments (1)

SharePoint 2101: Migrate List Views to SharePoint Online

Scenario:

We need to migrate List Views from SharePoint 2010 Server to SharePoint Online. We can use Web Services or Client Model. We decided to use Views Web Service.

Resolution:

First, we created a class which stores view information.

public class ListViewInfo

{

public ListViewInfo()

{

}

[DataMember]

public string listName { get; set; }

[DataMember]

public string viewName { get; set; }

[DataMember]

public string viewGUID { get; set; }

[DataMember]

public string openApplicationExtension { get; set; }

[DataMember]

public XmlNode viewFields { get; set; }

[DataMember]

public XmlNode viewQuery { get; set; }

[DataMember]

public XmlNode rowLimit { get; set; }

[DataMember]

public string type { get; set; }

[DataMember]

public bool makeViewDefault { get; set; }

[DataMember]

public XmlNode viewHeader { get; set; }

[DataMember]

public XmlNode viewBody { get; set; }

[DataMember]

public XmlNode viewFooter { get; set; }

[DataMember]

public XmlNode viewEmpty { get; set; }

[DataMember]

public XmlNode rowLimitExceeded { get; set; }

[DataMember]

public XmlNode viewToolBar { get; set; }

[DataMember]

public XmlNode viewAggregations { get; set; }

[DataMember]

public XmlNode viewFormat { get; set; }

}

We created two methods to get views information and to create views.

public static List<ListViewInfo> GetViewDetails(string listUrl)

{

List<ListViewInfo> listViewList = new List<ListViewInfo>();

ListViewInfo listView = new ListViewInfo();

string viewXML = string.Empty;

XmlDocument doc = new XmlDocument();

using (SPSite spSite = new SPSite(“SiteURL”))

{

using (SPWeb spWeb = spSite.OpenWeb())

{

SPList spList = spWeb.GetList(listUrl);

SPViewCollection spViewColl = spList.Views;

foreach (SPView spView in spViewColl)

{

viewXML = spView.HtmlSchemaXml;

doc.LoadXml(viewXML);

XmlNode root = doc.FirstChild;

listView.listName = spList.Title;

listView.viewName = root.Attributes[“DisplayName”].Value;

listView.viewGUID = root.Attributes[“Name”].Value;

listView.viewFields = root.SelectSingleNode(“/View/ViewFields”);

listView.viewQuery = root.SelectSingleNode(“/View/Query”);

listView.rowLimit = root.SelectSingleNode(“/View/RowLimit”);

listView.type = spView.Type;

listView.makeViewDefault = spView.DefaultView;

listView.viewHeader = root.SelectSingleNode(“/View/ViewHeader”);

listView.viewFooter = root.SelectSingleNode(“/View/ViewFooter”);

listView.viewBody = root.SelectSingleNode(“/View/ViewBody”);

listView.viewEmpty = root.SelectSingleNode(“/View/ViewEmpty”);

listView.viewToolBar = root.SelectSingleNode(“/View/ViewToolbar”);

listView.viewAggregations = root.SelectSingleNode(“/View/Aggregations”);

listView.viewFormat = root.SelectSingleNode(“/View/Formats”);

listView.openApplicationExtension = spView.OpenApplicationExtension;

listViewList.Add(listView);

listView = new ListViewInfo();

doc.RemoveAll();

}

}

}

return listViewList;

}

public static void createOrUpdateListView(List<ListViewInfo> listViewList)

{

ViewSvr.Views viewSrv = new ViewSvr.Views();

viewSrv.Credentials = System.Net.CredentialCache.DefaultCredentials;

viewSrv.Url = “http://SiteURL/_vti_bin/Views.asmx&#8221;;

if (listViewList.Count > 0)

{

string listName = listViewList[0].listName;

XmlNode viewColl = viewSrv.GetViewCollection(listName);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(“<root>” + viewColl.InnerXml + “</root>”);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

nsmgr.AddNamespace(“XYZ”, “http://schemas.microsoft.com/sharepoint/soap/&#8221;);

foreach (XPSListView listView in listViewList)

{

XmlNode foundView = xmlDoc.SelectSingleNode(@”/root/XYZ:View[@DisplayName='” + listView.viewName + “‘]”, nsmgr);

if (foundView == null)

{

//add view

XmlNode nv = viewSrv.AddView(listView.listName, listView.viewName, listView.viewFields, listView.viewQuery, listView.rowLimit, listView.type, listView.makeViewDefault);

}

else

{

//update view

XmlNode uv = viewSrv.UpdateViewHtml2(listView.listName, foundView.Attributes[“Name”].Value, null, listView.viewToolBar, listView.viewHeader, listView.viewBody, listView.viewFooter, listView.viewEmpty, listView.rowLimitExceeded, listView.viewQuery,listView.viewFields, listView.viewAggregations,listView.viewFormat,listView.rowLimit, listView.openApplicationExtension);                    }

foundView = null;

}

}

}

P.S. We have used UpdateViewHtml2 Method to update the view but not able to find viewProperties. Please let me know if anyone find the mapping.

Also, please add web reference of Views Web Service in your project and replace following line with your web reference

ViewSvr.Views viewSrv = new ViewSvr.Views();

Leave a Comment

SharePoint 2010: PowerShell Script to add / display list as webpart

Scenario:

We were required to add / display discussion forum / discussion board as a webpart on a publishing page.

Resolution:

We used “XsltListViewWebPart” webpart to accomplish above requirement

$webpart New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart

$webpart.ListId $list.ID;

$webpart.ViewGuid $list.DefaultView.ID.ToString();

$webpart.AllowClose $false;

$webpart.AllowConnect $false;

$webpart.AllowEdit $false;

$webpart.AllowHide $false;

$webpart.AllowMinimize $false;

$webpart.AllowZoneChange $false;

$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;

$webpartmanager=$web.GetLimitedWebPartManager($pageUrl,

[System.Web.UI.WebControls.WebParts.PersonalizationScope]

::Shared)

$webpartmanager.AddWebPart($webpart, “Zone 1”, 0);

You need to set following variables:

$list = Get the reference of list. In our case, it was discussion forum

$pageUrl = Url of the page

Leave a Comment

SharePoint 2010: PowerShell Script to create Discussion Board / Discussion Forum

Scenario:

We need to create discussion board / discussion forum using power shell script

Resolution:

$spWeb Get-SPWeb $webURL

$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DiscussionBoard

$spWeb.Lists.Add(“Discussion Forum”, “Custom List”, [int]$listTemplate)

Thanks to my friend Abdur Raheem 

Comments (1)

SharePoint 2007: Last Modified Date of Lists / Document Libraries

Scenario: We need to find the last access / modified date of all document libraries in a site collection.

Resolution:

Following code solved our issue

SPSite spSite = new
SPSite(“SiteCollectionURL”);

SPWeb spWeb = spSite.OpenWeb();

SPListCollection listColl = spWeb.Lists;

DateTime lastModifiedDate;

foreach (SPList list in listColl)

{     

if (list.BaseType == SPBaseType.DocumentLibrary)

{     

lastModifiedDate = list.LastItemModifiedDate;

Console.WriteLine(“Document Library Title:” + list.Title + “\nLast Modified Date:” + lastModifiedDate + “\n\n”);

}

}

P.S. Please change BaseType as per your requirement for Lists etc or remove this check all together.

Comments (1)

MOSS 2007: You cannot create alerts for lists for which users can only read their own items

We tried to set up alerts for users for a list with customized permissions. Following error appeared: 

You cannot create alerts for lists for which users can only read their own items. 

Resolution: 

Set up a workflow using SharePoint Designer which served same purpose.

Leave a Comment

Import Spreadsheet Error: Method ‘Post’ of object ‘IOWSPostData’ Failed

We were trying to import Excel sheet data into SharePoint and got following error:

 Method ‘Post’ of object ‘IOWSPostData’ Failed

 Resolution:

 Need to perform following steps: 

  • Go to “C:\Program Files\Microsoft Office\Office12\1033”.
  • Find “EXPTOOWS.XLA” and double click on it.
  • Press “Alt+F11” to display VB code editor.
  • Select code window and find “lVer = Application.SharePointVersion(URL)”
  • Add following line “lVer = 2”.
  • Save and close the window.
  • Restart and see if it worked.
  • If not, change “lVer = 3”.
  • Restart and see if it worked.

Comments (9)

Older Posts »