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("", "");

Advertisements

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)

Older Posts »