Posts Tagged SharePoint

SharePoint: Create Dynamic SPQuery

Scenario:

We need to develop a webpart that searches a list in SharePoint. The search criteria are based upon user input. A simplified image of search criteria is as under:


Resolution:

After googling, we found a great solution:
http://sharepointcamlhelper.codeplex.com/

Suppose we have two checkboxes and two radio button, then our query should be like this:

SPQuery oQuery = new SPQuery();
CAMLManager mgr = new CAMLManager();
mgr.QueryGroups.Add(new QueryGroup("ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
if (chkSelectGroup.Checked == true)
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, ddlSelectGroup.SelectedItem.Text));
}
if (chkSubject.Checked == true)
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Contains, txtSubject.Text));
}
if (rbAnnByEmail.SelectedIndex != -1)
{
if (rbAnnByEmail.SelectedItem.Text == "Yes")
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
}
if (rbAnnByEmail.SelectedItem.Text == "No")
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
}
}
mgr.OrderBy.Add(new OrderBy("ColumnName", false));
string camlQuery = mgr.GetFullCAML();
oQuery.Query = camlQuery;

Issue:
One thing that doesn’t seems to be working is “ViewFields”. We have added following lines to restrict our query to specific fields, but it doesn’t seems to be working.

cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));


Update:
If we add following code, ViewFields seems to work

spQuery.ViewFieldsOnly = true;
spQuery.ViewFields = cmgr.GetViewFields().Replace("", "").Replace("", "");

Comments (1)

SharePoint:InputFormTextBox: Validation Issue after postback

Scenario:

We have placed a “SharePoint:InputFormTextBox” text control on our webpart and wanted to make it a “Required Field”. We added an ASP “RequiredFieldValidator” which works fine when we click submit.

We have other controls on the form as well. If an event is occurred and any other controls postback, then, the validation on “SharePoint:InputFormTextBox” fails to work if we try to submit.

Resolution:

We look into the issue and tried to resolve by adding implementing different solutions but nothing was working. In the end, found a reply on MS SharePoint forum that was buried in other replies. Suggested setting the initial property of “RequiredFieldValidator”.

Turned out the when webpart postback, somehow, “<DIV></DIV>” is added in “SharePoint:InputFormTextBox”. You can check it by editing the control “SharePoint:InputFormTextBox” after postback from some other control.

We set

RequiredFieldValidator3.InitialValue = “<DIV></DIV>”

in every control server-side code that postback.

Source: http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/e9fbbd43-bef9-48fc-9a9f-0011514798e7/ Look at the last reply by “cschoenfeldt“.

Leave a Comment

SharePoint: Remove Page node from breadcrumb

Scenario:

We were using breadcrumb and wanted to remove “Pages” library link from breadcrumb.

Resolution:

The control we were using was:

<asp:SiteMapPath
SiteMapProvider=”SPContentMapProvider” id=”ContentMap” SkipLinkText=”” RenderCurrentNodeAsLink=”false”
NodeStyle-CssClass=”ms-sitemapdirectional” runat=”server”/>

One way of removing the “Pages” node is to use “CurrentNavSiteMapProviderNoEncode” instead of “SPContentMapProvider”. Also, make sure that you have checked “Show Pages” under “Site Settings -> Navigation ->Current Navigation”.

This approach has one drawback that if you are using OOTB Left Navigation, then, pages will also start appearing in the “Left Navigation”. In order to avoid this, we are left with jQuery. Below is the snippet to accomplish above.

First include jQuery in your page:

<script
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”></script>

Add you control as:

<asp:SiteMapPath
SiteMapProvider=”SPContentMapProvider” id=”ContentMap” SkipLinkText=”” RenderCurrentNodeAsLink=”false” 
NodeStyle-CssClass=”ms-sitemapdirectional” runat=”server”/>

Add following after the control

<script
type=”text/javascript”>

$(“Span[id=’ctl00_ContentMap’]”).children().each(function(){

if($(this).text()==’Pages’)

{

$(this).next().remove();

$(this).remove();

}

</script>

Leave a Comment

SharePoint: Reduce space b/w webparts

Scenario:

We need to reduce space b/w webparts

Resolution:

SharePoint add a div after webaprt with class “ms-PartSpacingVertical” which has margin-top property set to 12px. Add class with the same name and make margin-top zero. In our scenario, webparts were added inside div with id “middle”, so we added following class

#middle .ms-PartSpacingVertical{

    margin-top: 0px !important;

}

Leave a Comment

SharePoint: Get List of Folders and Subfolders

Scenario:

A question on MSN requested for code to populate two drop downs from a document library. “DropDown A” should contain list of all the folders at the root. If we select any folder from “DropDown A”, “DropDown B” should be populated with list of all folders in the selected folder.

Resolution:

To get the list of all the folders at root, following code will help:

using(SPSite oSite = new SPSite(“Site_URL”))

{

SPWeb oWeb = oSite.OpenWeb();

SPList oList = oWeb.Lists[“DocumentLibraryName”];

SPFolderCollection oFolders = oList.RootFolder.SubFolders;

foreach (SPFolder oFolder in oFolders)

{

if (!oFolder.Name.Equals(“Forms”))

{

//use oFolder.Name property to populate DropDown A

}

}

}

Once selected, following code will populate “DropDown B” with list of subfolders:

SPFolder selFolder = oList.RootFolder.SubFolders[oList.Title + “/” + “Selected Folder Name”];

SPFolderCollection selSubFolders = selFolder.SubFolders;

foreach (SPFolder selSubFolder in selSubFolders)

{

//use subFolder.Name property to populate DropDown B

}

Comments (7)

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: Workflow Migration

This is the second post related to SharePoint workflows. In first post, we have looked at the implementation details of the OOTB and SPD workflows. In the this post, we will look at the migration of OOTB and SPD 2010 workflows from one list to other or from one form to other.

Migration of Workflows: In order to migrate workflow from one site to other or from one server to other, we need to take care of three or four files mentioned previously. These files contain hard coded list and site GUID along with other details.

In case of OOTB workflow, instance details are stored in content database and can be seen in the table of “WorkflowAssociation”. Column “InstantiationParams” contains the details of users, order, due date etc. in XML format while other columns have of site / web / list IDs, name / description of workflow, task / history list IDs and names.

In case of SPD workflows, the definitions of workflow files are stored in list “workflows” created at the root of each site and web. In case of SPD 2007, files generated for a workflow are xoml, xoml.wfconfig.xml, aspx and xoml.rules if rules are used in the workflow.

Moving back to our original topic of migration, we need to change the GUID / ID of lists that are coded in xoml.wfconfig.xml file and you are done. A lot of third party workflow migrators are available but if you are interested in an open source solution, codeplex gives you hope.

http://spwflmigrator.codeplex.com/

This is a good resource for starting. Thanks rane_peinl J

In case of SPD 2010 workflows, files generated for a workflow are xoml, xoml.wfconfig.xml, xsn and xoml.rules if rules are used in the workflow. The big difference is the implementation of initiation form. In SPD 2007 workflow, initiation form is aspx page while in case of SPD 2010, initiation form is an InfoPath form. If you export the InfoPath form, add .cab with the form name and extract it, you will be able to find files that are generated as part of InfoPath form.

When we tried to migrate SPD 2010 workflow from one list to other, we were able to read xoml.wfconfig.xml and replace all the related IDs with the destination but we are unable to create a working InfoPath form. Although InfoPath is migrated successfully but when we try to start workflow, “Form has some error” message appeared. Creating a working initiation form for the migrated workflow doesn’t seem to be happening.

We do have third party solution that claims to migrate SPD 2010 workflow but I am waiting for their support response.

Comments (1)

SharePoint: Workflows Implementation

In this article we have tried to explore implementation details of workflows in SharePoint 2007 and 2010. SharePoint provides us with three flavors of workflows, namely, Out Of the Box (OOTB), SharePoint Designer (SPD) and Visual Studio workflows. We will discuss implementation details of OOTB and SPD workflow w.r.t. to SharePoint 2007 (MOSS) and SharePoint 2010.

Out Of the Box Workflows: OOTB workflows are workflows which come with SharePoint server installation.

In MOSS, these workflows come in rigid form. You can’t customize them. The only option available is to create instance of OOTB workflows in different list or libraries and run them as they are. The instance details of OOTB workflows are stored in content database.

In SharePoint 2010, OOTB workflows are also termed as “Globally Reusable Workflows”. Other than “Three State workflow” which comes with SharePoint Foundations (free of cost), all other workflows comes with server or enterprise licenses. Great news is that these workflows can be customized using SPD 2010. The definition of OOTB workflows can be found using SPD 2010. Open site in SPD 2010 and select “All Files”. In the right pane, select “_catalogs” and then “wfpub”, you will be able to find OOTB workflows.

When you customize “Globally Reusable Workflow”, the definition of newly created workflow is stored in a list “workflows”.

SharePoint Designer Workflows: Workflows created using SPD are termed as SPD workflows. They are also called declarative workflows because they consist of Extensible Markup Language (XOML) rather than compile code as in the case of Visual Studio workflows.

In SPD 2007, we have only one type of workflow, namely, List workflow. In case of SPD 2010, we have three different types of workflows, namely, List, Reusable and Site workflows. When we create a new SPD workflow, a list “workflows” is created if don’t exist. Workflows list is not visible in SharePoint UI and can be accessed via SPD or SharePoint Object Model (OM). We can traverse workflows list using OM as under:

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

{

SPWeb spWeb = spSite.OpenWeb();

SPList spList = spWeb.Lists[“workflows”];

foreach (SPListItem spListItem in spList.Items)

{

Console.WriteLine(spListItem.Name);

}

}

In case of SPD, open site in SPD and select “All Files”. In the right pane, select “workflows” and you will be able to find SPD workflows

Workflows list contains all the files related to a particular workflow. For each SPD workflow, three or four files are created depending upon the existence rules or conditions.

  • xoml – Contains all the activities included in the workflow in Extensible Application Markup Language (XAML) format
  • xoml.wfconfig.xml – Contains details of the list or site GUIDs and start configuration of the workflow
  • xsn or aspx – In MOSS, an aspx page is created for starting workflow. In SharePoint 2010, we have InfoPath form in place of aspx page
  • xoml.rules – Contains workflow rules if used

In the next post we will discuss the issues relating to migration of SPD workflows.

Comments (1)

SharePoint 2010 event @ Hajvery University

I spoke at a session organized by Microsoft Pakistan and Hajvery University of Pakistan. The event held on Monday, June 27th, 2011. I presented on SharePoint 2010 for students with CS and management background. The snaps of the event are available @ https://picasaweb.google.com/farhan.faiz/SharePoint2010EventHU.

Thanks to Mubhashir Saleemi, people at HU and people at Microsoft Pakistan for the session.

Comments (1)

SharePoint: Incompatible Web Part markup detected. Use *.dwp Web Part XML instead of *.webpart Web Part XML

Scenario:

We deployed a webpart and when tried it to add it on the page, following error occurred:

Incompatible Web Part markup detected. Use *.dwp Web Part XML instead of *.webpart Web Part XML.

Resolution:

This error occurs when you have not inherited your class from “System.Web.UI.WebControls.WebParts.WebPart”. We have inheritated our class from “Microsoft.Office.Server.WebAnalytics.Reporting.WhatsPopularWebPart” class as we were customizing OOTB “WhatsPopularWebPart”. The resolution is to create your own “.dwp” file. Copy pate following XML, make appropriate changes and save it with “.dwp” extension.

<?xml version=”1.0″?>

<WebPart xmlns=”http://schemas.microsoft.com/WebPart/v2″&gt;

<Assembly>[AssemblyName],Version=[X.X.X.X], Culture=[XXXXX],PublicKeyToken=[XXXXXXXX]</Assembly>

<TypeName>WebPartNamespace.WebPartClassName</TypeName>

<Title>WebPartTitle</Title>

<Description>WebPartDescription</Description>

</WebPart>

One quick way is to open “.webpart” file and replace values.

Second step is to go to 14 hives or 12 hives [C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES], found the feature folder. Delete “.webpart” file and paste “.dwp” file. Repeat the same process in the SharePoint installation. Go to Site Collection Settings-> Web parts (under Galleries). Delete “.webpart” file and upload “.dwp” file.

Source:

http://blog.sharepointdevelopment.nl/post/Unable-to-add-selected-Web-Part-Incompatible-Web-Part-markup-detected.aspx

Comments (2)

Older Posts »