Remembering Ancestors in MvcSiteMapProvider - mvcsitemapprovider

I have a general need to maintain a reference to my ancestors as I traverse down the sitemap.
Mvc.sitemap
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">
<mvcSiteMapNode title="Home" controller="Home" action="Index" >
<mvcSiteMapNode title="Products" url="~/Home/Products" roles="*">
<mvcSiteMapNode title="Harvest MAX" url="~/Home/Products/HarvestMAX" >
<mvcSiteMapNode title="Policies" url="~/Home/Products/HarvestMAX/Policy/List" productType="HarvestMax" type="P" typeFullName="AACOBusinessModel.AACO.HarvestMax.Policy" roles="*">
<mvcSiteMapNode title="Policy" controller="Object" action="Details" typeName="Policy" typeFullName="AACOBusinessModel.AACO.HarvestMax.Policy" preservedRouteParameters="id" roles="*">
<mvcSiteMapNode title="Counties" controller="Object" action="List" collection="Counties" roles="*">
<mvcSiteMapNode title="County" controller="Object" action="Details" typeName="County" typeFullName="*" preservedRouteParameters="id" roles="*">
<mvcSiteMapNode title="Land Units" controller="Object" action="List" collection="LandUnits" roles="*">
<mvcSiteMapNode title="Land Unit" controller="Object" action="Details" typeName="LandUnit" typeFullName="AACOBusinessModel.AACO.LandUnit" preservedRouteParameters="id" roles="*">
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMap>
Controller
[SiteMapTitle("Label")]
public ActionResult Details(string typeFullName, decimal id)
{
return View(AACOBusinessModel.AACO.VersionedObject.GetObject( typeFullName?.ToType() ?? Startup.CurrentType,
ApplicationSignInManager.UserCredentials.SessionId,
id));
}
There are many reasons I want this, but here are some specific examples.
Example 1: Vanishing ID's
Let's say the url that got me to the Policy node is http://localhost:36695/AACOAgentPortal/details/Policy/814861364767412.
Once I navigate down past that to the County node, my breadcrumbs looks like this:
However if I hover over the Policy breadcrumb, the url given is http://localhost:36695/AACOAgentPortal/Object/Details?typeName=Policy&typeFullName=AACOBusinessModel.AACO.HarvestMax.Policy. As you can see, the id is gone.
Example 2: Vanishing Titles
As you can see in my controller, I'm telling mvc sitemap that I want to use the Label property to display the node title. It does that when it's the leaf node:
But once I go past that, it disappears:
Both of these issues may have a common cause. There are other reasons why I may want a reference to an ancestor along the breadcrumb trail, but these are two concrete ones to exemplify the issue.

I solve this by keeping my objects in a hierarchy in session, and each object has the same key that it's node has so that it can find the node upon processing each request.
MenuItems.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AtlasKernelBusinessModel;
using MvcSiteMapProvider;
using CommonBusinessModel.Commands;
using CommonBusinessModel.Extensions;
using CommonBusinessModel.Security;
namespace AtlasMvcWebsite.Code
{
[Serializable]
public class MenuItems : Dictionary<string, MenuItem>
{
#region Properties
public IEnumerable<Command> AvailableCommands
{
get
{
return CurrentItem?.Commandable?.AvailableCommands() ?? new List<Command>();
}
}
/// <summary>
/// Each User has his own copy because it has to track his travel through the hierarchy
/// </summary>
public static MenuItems Current
{
get
{
return (MenuItems)(HttpContext.Current.Session["MenuItems"] =
HttpContext.Current.Session["MenuItems"] ??
new MenuItems());
}
}
private MenuItem currentItem;
public MenuItem CurrentItem
{
get
{
return currentItem =
CurrentNode == null ?
null :
this[CurrentNode.Key] =
ContainsKey(CurrentNode.Key) ?
this[CurrentNode.Key] :
new MenuItem(CurrentNode,
CurrentNode.ParentNode != null ? this[CurrentNode.ParentNode.Key] : null);
}
}
public ISiteMapNode CurrentNode { get; private set; }
#endregion
#region Methods
private void Build()
{
Build(SiteMaps.Current.RootNode);
}
private void Build(ISiteMapNode node)
{
foreach (var childNode in node.ChildNodes)
{
foreach (var att in node.Attributes.Where(a => !childNode.Attributes.Any(na => na.Key == a.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value))
{
switch (att.Key)
{
case "productType":
case "typeFullName":
case "typeName":
childNode.Attributes[att.Key] = att.Value;
childNode.RouteValues[att.Key] = att.Value;
break;
}
}
Build(childNode);
}
}
/// <summary>
/// We finally have an object from the details controller and we want to set it to the current menu item
/// </summary>
/// <param name="versionedObject"></param>
/// <returns></returns>
public AtlasKernelBusinessModel.VersionedObject Set(AtlasKernelBusinessModel.VersionedObject versionedObject)
{
((ICommandable)versionedObject).UserAccess = User.Credentials.BusinessObjectAccessFor(versionedObject.ObjectType());
if (CurrentItem != null)
this[CurrentItem.Node.Key].Object = versionedObject;
//else
// for commands
//SiteMapNodeObjects[SiteMapNodeObjects.Last().Key] = versionedObject;
return versionedObject;
}
public void Sync()
{
//Build();
CurrentNode = SiteMaps.Current.CurrentNode;//cache value of current node
Values.ToList().ForEach(m => m.Sync());
}
#endregion
}
}
MenuItem.cs
using AtlasKernelBusinessModel;
using MvcSiteMapProvider;
using System;
using CommonBusinessModel.Commands;
namespace AtlasMvcWebsite.Code
{
[Serializable]
public class MenuItem
{
#region Constructors
public MenuItem(ISiteMapNode node, MenuItem parent)
{
Node = node;
Parent = parent;
}
#endregion
public ISiteMapNode Node;
public readonly MenuItem Parent;
private ICommandable commandable;
public ICommandable Commandable
{
get
{
return commandable;
}
}
private VersionedObject #object;
public VersionedObject Object
{
get
{
return #object;
}
set
{
#object = value;
Type = #object.GetType();
commandable = (ICommandable)#object;
Sync();
}
}
public Type Type;
public void Sync()
{
// sync the node to the object
if (Object == null) return;
Node = SiteMaps.Current.FindSiteMapNodeFromKey(Node.Key);
Node.Title = Type.GetProperty("Label").GetValue(Object).ToString();
Node.RouteValues["id"] = Object.Id;
}
public override string ToString()
{
return $"{Parent?.Node?.Title}.{Node.Title}";
}
}
}
Usage Example
var menuItem = MenuItems.Current.CurrentItem; // ensure current item exists
if (menuItem != null)
{
<!-- CHILD ITEM MENU -->
#Html.MvcSiteMap().Menu(menuItem.Node, true, false, 1)
<!-- COMMAND BUTTONS -->
if (!viewModel.ReadOnly)
{
#Html.DisplayFor(m => menuItem.Commandable.BusinessOperations.Commands)
}

When using preservedRouteParameters, the source of the values it retrieves is from the current request. Therefore, you cannot reuse id for a different purpose if you expect to navigate up the hierarchy. Also, you must ensure that all ancestral preservedRouteParameters are included in the current request or the URLs will not be built correctly.
There is a demo of how to use preservedRouteParameters here: https://github.com/NightOwl888/MvcSiteMapProvider-Remember-User-Position.

Related

How to get DAC record from Note Table

Does anyone have a code snippet on how to go from RefNoteId => DAC when I dont know what dac type the note is attached to?
I have made it this far (row.RefNoteID is what I am starting from)
Note note = PXSelect<Note, Where<Note.noteID, Equal<Required<Note.noteID>>>>.Select(this, row.RefNoteID);
Type recordType = Type.GetType(note.EntityType);
PXCache recordCache = Caches[recordType];
How can I now do a PXSelect<recordType, Where<recodType.noteID, Equal<Required<recordType.noteID>>>>.Select(GRAPH) ? The recordType could be any DAC in the system that has a noteID.
Thanks
The below code works for me and it is based on the way Acumatica gets the record inside the PXRefNoteSelectorAttribute.PrimaryRow_RowPersisted.
The problem with this approach is that this will work for Header entities like SOOrder, INRegister, SOInvoice, SOShipment, and others. But for "detail" entities like SOLine, INTran, and others this approach will work only if that corresponding record has some Note related to Text/File. Acumatica is adding records corresponding to their NoteID into the Note table only if that detail records have some Note/Text. My best guess is that this is done in order to avoid over-spamming the Note table.
using PX.Data;
using PX.Objects.SO;
using System;
using System.Collections;
using System.Linq;
using System.Web.Compilation;
namespace SearchByNoteID
{
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
public PXAction<SOOrder> searchByNoteID;
[PXUIField(DisplayName ="Search by Note ID")]
[PXButton(CommitChanges = true)]
public virtual IEnumerable SearchByNoteID(PXAdapter adapter)
{
var order = adapter.Get<SOOrder>().FirstOrDefault();
if(order!=null)
{
//
//...
//
Guid? noteID = GetNoteID();
object record = GetRecordByNoteID(noteID);
//
//... do whatever you want with the record
//
}
return adapter.Get();
}
protected object GetRecordByNoteID(Guid? noteID)
{
var type = GetEntityType(this.Base, noteID);
if(type==null) return null;
object entityRow = new EntityHelper(this.Base).GetEntityRow(type, noteID);
return entityRow;
}
protected Type GetEntityType(PXGraph graph, Guid? noteID)
{
if (noteID == null)
{
return null;
}
Note note = PXSelectBase<Note, PXSelect<Note, Where<Note.noteID, Equal<Required<Note.noteID>>>>.Config>.SelectWindowed(graph, 0, 1, new object[]
{
noteID
});
if (note == null || string.IsNullOrEmpty(note.EntityType))
{
return null;
}
return PXBuildManager.GetType(note.EntityType, false);
}
}
}

How to create a new model?

I wish I could do the following: #Html.MvcSiteMap().PageMenu() instead of #Html.MvcSiteMap().Menu("MenuBtnGroup").
Mainly because of the filters.
I wish that a certain link to appear only on the page or sitemap and not in the main application menu
<mvcSiteMapNode title="Usuários" controller="Usuarios" action="Index">
<mvcSiteMapNode title="Novo Usuário" action="Novo" visibility="SiteMapPathHelper,PAGEMENU-ONLY,!*" />
<mvcSiteMapNode title="Detalhes" action="Detalhes" visibility="SiteMapPathHelper,!*" dynamicNodeProvider="UsuarioDynamicNodeProvider, Web">
<mvcSiteMapNode title="Editar" action="Editar" inheritedRouteParameters="id" />
</mvcSiteMapNode>
</mvcSiteMapNode>
This functionality isn't built-in (yet), but there is a way to make it happen by building your own visibility provider and using the SourceMetaData to pass the name of the menu into the visibility logic.
/// <summary>
/// Filtered SiteMapNode Visibility Provider for use with named controls.
///
/// Rules are parsed left-to-right, first match wins. Asterisk can be used to match any control or any control name. Exclamation mark can be used to negate a match.
/// </summary>
public class CustomFilteredSiteMapNodeVisibilityProvider
: SiteMapNodeVisibilityProviderBase
{
#region ISiteMapNodeVisibilityProvider Members
/// <summary>
/// Determines whether the node is visible.
/// </summary>
/// <param name="node">The node.</param>
/// <param name="sourceMetadata">The source metadata.</param>
/// <returns>
/// <c>true</c> if the specified node is visible; otherwise, <c>false</c>.
/// </returns>
public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata)
{
// Is a visibility attribute specified?
string visibility = string.Empty;
if (node.Attributes.ContainsKey("visibility"))
{
visibility = node.Attributes["visibility"].GetType().Equals(typeof(string)) ? node.Attributes["visibility"].ToString() : string.Empty;
}
if (string.IsNullOrEmpty(visibility))
{
return true;
}
visibility = visibility.Trim();
// Check for the source HtmlHelper
if (sourceMetadata["HtmlHelper"] == null)
{
return true;
}
string htmlHelper = sourceMetadata["HtmlHelper"].ToString();
htmlHelper = htmlHelper.Substring(htmlHelper.LastIndexOf(".") + 1);
string name = sourceMetadata["name"].ToString();
// All set. Now parse the visibility variable.
foreach (string visibilityKeyword in visibility.Split(new[] { ',', ';' }))
{
if (visibilityKeyword == htmlHelper || visibilityKeyword == name || visibilityKeyword == "*")
{
return true;
}
else if (visibilityKeyword == "!" + htmlHelper || visibilityKeyword == "!" + name || visibilityKeyword == "!*")
{
return false;
}
}
// Still nothing? Then it's OK!
return true;
}
#endregion
}
Then you can just name each of your menus by giving them a "name" SourceMetadata attribute.
#Html.MvcSiteMap().Menu(new { name = "MainMenu" })
#Html.MvcSiteMap().Menu(new { name = "PageMenu" })
And then use the CustomFilteredSiteMapVisibilityProvider instead of the FilteredVisibilityProvider in your configuration. See this answer for a complete example.

Add wrapper to widgets only in a specific zone

I'm adding a custom wrapper to widgets in placement.info like this:
<Match ContentType="Widget">
<Place Parts_Common_Body="Content:5;Wrapper=Wrapper_AsideWidget" />
</Match>
This works just fine, but I need to to limit the application of the custom wrapper to only widgets in a few specific zones. Right now they're being applied to widgets in all zones. What's the best way to achieve this? It would be perfect if the Match element could be scoped to a zone but I don't think that's possible.
Any advice or suggestions?
UPDATE
Here's the final solution I came up with. It applies the custom wrapper to any widgets in the aside zones. Just dropped the class into the theme.
public class AsideWidgetShapeProvider : IShapeTableProvider
{
public void Discover(ShapeTableBuilder builder)
{
builder.Describe("Widget")
.OnDisplaying(displaying =>
{
var shape = displaying.Shape;
ContentItem contentItem = shape.ContentItem;
if (contentItem != null)
{
var zoneName = contentItem.As<WidgetPart>().Zone;
if (zoneName == "AsideFirst" || zoneName == "AsideSecond")
{
shape.Metadata.Wrappers.Add("Wrapper_AsideWidget");
}
}
});
}
}
You can create a Shape Table Provider that describes the behavior of the Parts_Common_Body shape and applies your wrapper conditionally. Just add a class such as the following to your module, and Orchard will process it when it builds the shape table.
Example:
using Orchard.ContentManagement;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Widgets.Models;
namespace MyModule {
public class ShapeTable : IShapeTableProvider {
public void Discover(ShapeTableBuilder builder) {
builder.Describe("Parts_Common_Body")
.OnDisplaying(ctx => {
var shape = ctx.Shape;
// Parts_Common_Body has a ContentPart property, so you can
// do this to get at the content item.
var contentItem = ((IContent)shape.ContentPart).ContentItem;
if (contentItem.ContentType == "Widget") { // content type to check for
var widgetPart = contentItem.As<WidgetPart>();
if (widgetPart.Zone == "AsideFirst") { // zone to check for
// Condition is met, let's add the wrapper.
ctx.ShapeMetadata.Wrappers.Add("Wrapper_AsideWidget");
}
}
});
}
}
}

Orchard Tokens ->

Im getting a headache from the Orchard Token Providers and any help would be a blessing.
Ive mostly been copying the Comments Tokens.
The target is for an email to be sent when a 'Question' content item is published.
Here is my solution for the Content Type 'Question':
public interface ITokenProvider : IEventHandler
{
void Describe(dynamic context);
void Evaluate(dynamic context);
}
public class QuestionTokens : ITokenProvider
{
public QuestionTokens()
{
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(dynamic context)
{
//presume this is correct
context.For("Content", T("Content Items"), T("Content Items"))
.Token("QuestionText", T("Question Text"), T("Text of the question"))
.Token("QuestionAuthor", T("Question Author"), T("Author of the question"));
//presume this is incorrect? correct for the content type?
context.For("Question", T("Questions"), T("Questions from users"))
.Token("QuestionText", T("Question Text"), T("Text of the question"))
.Token("QuestionAuthor", T("Question Author"), T("Author of the question"));
}
public void Evaluate(dynamic context)
{
Func<IContent, object> questionTextAccessorFromContent = (content) => {
var part = content.As<QuestionPart>();
return part.QuestionText;
};
Func<QuestionPart, object> questionTextAccessor = (part) =>
{
return part.QuestionText;
};
Func<IContent, object> authorAccessorFromContent = (content) => {
var part = content.As<QuestionPart>();
return part.Author;
};
Func<QuestionPart, object> authorAccessor = (part) =>
{
return part.Author;
};
//doesnt work
context.For<IContent>("Content")
.Token("QuestionText", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.QuestionText))
.Token("QuestionAuthor", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.Author));
//doesnt work
context.For<IContent>("Question")
.Token("QuestionText", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.QuestionText))
.Token("QuestionAuthor", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.Author));
//doesnt work
context.For<QuestionPart>("Question")
.Token("QuestionText", (Func<QuestionPart, object>)
(content => content.Record.QuestionText))
.Token("Author", (Func<QuestionPart, object>)
(content => content.Record.Author)); ;
}
private static string QuestionText(IContent question)
{
var questionPart = question.As<QuestionPart>();
return questionPart.QuestionText;
}
private static string Author(IContent question)
{
var questionPart = question.As<QuestionPart>();
return questionPart.Author;
}
}
This is the Action (when the Question is published), it sends a email with the body text:
(updated to test Medeiros suggestion)
<p>A question has been created by: {Content.QuestionAuthor}</p>
<p>A question has been created by: {Content.QuestionAuthor.Text}</p>
<p>A question has been created by: {Content.QuestionAuthor.Value}</p>
<p>A question has been created by: {Question.QuestionAuthor}</p>
<p>A question has been created by: {Question.QuestionAuthor.Text}</p>
<p>A question has been created by: {Question.QuestionAuthor.Value}</p>
<p>Message: {Content.QuestionText}</p>
<p>Message: {Content.QuestionText.Text}</p>
<p>Message: {Content.QuestionText.Value}</p>
<p>Message: {Question.QuestionText}</p>
<p>Message: {Question.QuestionText.Text}</p>
<p>Message: {Question.QuestionText.Value}</p>
All 'my' tokens are replaced with blank text. Other tokens like: {Content.ContentType} {User.Email} work just fine. Any mistakes or hints that anyone notices will be very useful.
Thanks, Matt
IVe re-written the token provider and it works sort of... (and now works completely)
public class QuestionTokens : Orchard.Tokens.ITokenProvider
{
private readonly IContentManager contentManager;
private readonly IWorkContextAccessor workContextAccessor;
public QuestionTokens(IContentManager contentManager, IWorkContextAccessor workContextAccessor)
{
this.workContextAccessor = workContextAccessor;
this.contentManager = contentManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
/// <summary>
/// Describes the specified context.
/// </summary>
/// <param name="context">The context.</param>
public void Describe(DescribeContext context)
{
context.For("Content", T("Content Items"), T("Content Items"))
.Token("QuestionText", T("Question Text"), T("Text of the question"))
.Token("QuestionAuthor", T("Question Author"), T("Author of the question"));
}
/// <summary>
/// Evaluates the specified context.
/// </summary>
/// <param name="context">The context.</param>
public void Evaluate(EvaluateContext context)
{
context.For<IContent>("Content")
.Token("QuestionText", content => QuestionText(content, context))
.Token("QuestionAuthor", content => Author(content, context));
}
private string QuestionText(IContent question, EvaluateContext context)
{
var questionPart = question.As<QuestionPart>();
return questionPart.QuestionText;
}
private string Author(IContent question, EvaluateContext context)
{
var questionPart = question.As<QuestionPart>();
return questionPart.Author;
}
}
I can now debug the code at least in the private QuestionText and Author methods. The value retrieved is 'null' due to the published event firing when this line is fired in the controller:
services.ContentManager.Create("Question");
so the content item has not been populated and the 'published' event is firing before its life has begun. Time to play about with its content type settings a bit to see if that makes a difference...
Finally all working as needed. The Events section 'published' event is still happening when i dont expect it to so I've changed the life of the content.
From a controller: (some code into a service removed so I don't have a post more code)
var item = services.ContentManager.New<Models.QuestionPart>("Question");
this.TryUpdateModel(item);
services.ContentManager.Create(item.ContentItem); //where the event is firing
Services.ContentManager.Publish(item.ContentItem); //where i expected the event to fire :)
Originally it was create, update and save. But the events restricted that. So now its new, update and create.
As for the tokens. They are simply:
{Content.QuestionAuthor} and {Content.QuestionText}. Nice and simple when everything is working, and everything is right.
Matthew, try adding a .Value for the: {Content.QuestionAuthor.Value}
If this doesn't work, try .Text.
Hope this helps.
The code in your original question looks accurate. You follow the correct Orchard Events bus convention of creating an interface inheriting from IEventHandler link.
Your change in the controller code is likely the reason you are getting values. My experience debugging the code is that it depends on what Event your rule is subscribing to. For example an event of type "Content Created" hits the debugger in the Evaluate method but all custom part properties are null. But an event of "When a Custom Form is submitted" hits and has values populated.
I spent a full day troubleshooting my code which looks similar to yours above and it wasn't until I started testing the other event types in my rule that it had data populated in the private functions.
Here's my code:
public interface ITokenProvider : IEventHandler
{
void Describe(dynamic context);
void Evaluate(dynamic context);
}
public class PersonTokens : ITokenProvider
{
private readonly IContentManager _contentManager;
public Localizer T { get; set; }
public PersonTokens(IContentManager contentManager)
{
_contentManager = contentManager;
T = NullLocalizer.Instance;
}
public void Describe(dynamic context)
{
context.For("Content", T("Content Items"), T("Content Items"))
.Token("FirstName", T("First Name"), T("The Person's First Name"))
.Token("LastName", T("Last Name"), T("The Person's Last Name"))
.Token("EmailAddress", T("Email Address"), T("The Person's Email Address"))
.Token("PhoneNumber", T("Phone Number"), T("The Person's Phone Number"));
}
public void Evaluate(dynamic context)
{
// Orchard.Tokens.Implementation.TokenManager.EvaluateContextImpl
context.For<IContent>("Content")
.Token("FirstName", (Func<IContent, object>)FirstName)
.Token("LastName", (Func<IContent, object>)(content => content.As<PersonPart>().Record.LastName))
.Token("EmailAddress", (Func<IContent, object>)(content => content.As<PersonPart>().EmailAddress))
.Token("PhoneNumber", (Func<IContent, object>)PhoneNumber); // left the PhoneNumber out of sample
}
private string FirstName(IContent person)
{
var personPart = person.As<PersonPart>();
return personPart.Record.FirstName;
}
}

sharepoint custom workflow activity is doing nothing, not even log messages

I am trying to create a custom workflow activity which gives me the list of recipients that I am storing in list. But once I deploy and start the workflow nothing is happening, not even log messages are coming. So i tried to debug the code but breakpoints are not set and I am getting the error "The Breakpoint will not currently be hit. No symbols have been loaded for this document." Can anyone please help me to deal with this issue.
Below are the steps I have followed in creating this activity.
1. created a workflow activity library.
(please find my code file attached)
added .dll to GAC
updated web.config and WSS.actions files.
Now I see the action in designer, so i have created a workflow using designer.
strted the workflow manually on an item.
Here nothing is happening, not even an error. Please let me know if you need any further information.
Please find the code below.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using System.Diagnostics;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
namespace CustomWorkflowActivityLibrary
{
public partial class CustomWorkflowActivity: SequenceActivity
{
SPList _list;
private EventLog _log;
SPFieldUserValueCollection objUserFieldValueCol;
string semailsettingKeyword1;
string semailsettingKeyword2;
public CustomWorkflowActivity()
{
InitializeComponent();
}
public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(CustomWorkflowActivity));
[DescriptionAttribute("__Context")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public WorkflowContext __Context
{
get { return ((WorkflowContext)(base.GetValue(CustomWorkflowActivity.__ContextProperty))); }
set { base.SetValue(CustomWorkflowActivity.__ContextProperty, value); }
}
public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof(string), typeof(CustomWorkflowActivity));
[DescriptionAttribute("ListId")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string ListId
{
get { return ((string)(base.GetValue(CustomWorkflowActivity.ListIdProperty))); }
set { base.SetValue(CustomWorkflowActivity.ListIdProperty, value); }
}
public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof(int), typeof(CustomWorkflowActivity));
[DescriptionAttribute("ListItem")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public int ListItem
{
get { return ((int)(base.GetValue(CustomWorkflowActivity.ListItemProperty))); }
set { base.SetValue(CustomWorkflowActivity.ListItemProperty, value); }
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
_log = new EventLog("Add Description");
_log.Source = "Share Point Workflows";
try
{
//Execute method as a elevated method
SPSecurity.CodeToRunElevated elevatedExecuteMethod = new SPSecurity.CodeToRunElevated(ExecuteMethod);
SPSecurity.RunWithElevatedPrivileges(elevatedExecuteMethod);
}
catch (Exception ex)
{
_log.WriteEntry("Error" + ex.Message.ToString(), EventLogEntryType.Error);
}
return ActivityExecutionStatus.Closed;
}
private void ExecuteMethod()
{
try
{
//retrieveing the Site object
SPSite _site = new SPSite(__Context.Site.Url);
//retrieveing the Web object
SPWeb _web = (SPWeb)(__Context.Web);
//retrieveing the list object
_list = _web.Lists[new Guid(this.ListId)];
//retrieveing the list item object
SPListItem _listItem = _list.GetItemById(this.ListItem);
_site.AllowUnsafeUpdates = true;
_web.AllowUnsafeUpdates = true;
string semailsubject = _listItem["E-Mail Subject"].ToString();
string semailfrom = _listItem["emailfrom"].ToString();
_log = new EventLog("get vendor info");
_log.WriteEntry("semailsubject");
_log.WriteEntry("semailfrom");
/* _listItem.Update();
_list.Update();
_site.AllowUnsafeUpdates = false;
_web.AllowUnsafeUpdates = false;*/
using (SPSite mysite = new SPSite("http://dlglobaltest.dl.com/Admin/IT/Application%20Development%20Group/LibraryEmailDistribution"))
{
using (SPWeb myweb = mysite.OpenWeb())
{
SPList settingsList = myweb.Lists["EmailDistributionSettings"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='Sender' /><Value Type='Text'>" + semailfrom + "</Value></Eq></Where>";
SPListItemCollection ColListItems = settingsList.GetItems(oQuery);
foreach (SPListItem oListItem in ColListItems)
{
semailsettingKeyword1 = oListItem["Keyword1"].ToString();
semailsettingKeyword2 = oListItem["Keyword2"].ToString();
//SPFieldUserValue objUserFieldValue = new SPFieldUserValue(myweb, oListItem["Recipients"].ToString());
if ((semailsubject.Contains(semailsettingKeyword1)) || (semailsubject.Contains(semailsettingKeyword2)))
{
objUserFieldValueCol = new SPFieldUserValueCollection(myweb, oListItem["Recipients"].ToString());
_log = new EventLog(objUserFieldValueCol.ToString());
}
}
}
}
}
catch (Exception ex)
{ }
}
}
}
Web.Config:
<authorizedType Assembly="CustomWorkflowActivityLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a95e146fc1062337" Namespace="CustomWorkflowActivityLibrary" TypeName="*" Authorized="True" />
WSS.Actions:
<Action Name="Get Recipients"
ClassName="CustomWorkflowActivityLibrary.CustomWorkflowActivity"
Assembly="CustomWorkflowActivityLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a95e146fc1062337"
AppliesTo="all" Category="Custom">
<RuleDesigner Sentence="Get Recipients for %1 ">
<FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
</RuleDesigner>
<Parameters>
<Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext" Direction="In" />
<Parameter Name="ListId" Type="System.String, mscorlib" Direction="In" />
<Parameter Name="ListItem" Type="System.Int32, mscorlib" Direction="In" />
</Parameters>
</Action>
Thanks,
I am not sure if this will help but you could try changing the following line in your code from this:
SPSecurity.RunWithElevatedPrivileges(elevatedExecuteMethod);
To this:
SPSecurity.RunWithElevatedPrivileges(delegate(){
ExecuteMethod();
});
Another shot-in-the-dark reply:
Try changing the class you're inheriting ( http://msdn.microsoft.com/en-us/library/ms173149(v=VS.80).aspx ) from SequenceActivity to Activity (SequenceActivity inherits from CompositeActivity, which itself inherits from Activity. See: http://msdn.microsoft.com/en-us/library/system.workflow.activities.sequenceactivity(v=VS.90).aspx )
If that doesn't work, try removing your constructor entirely. You should be able to use the base (Sequence)Activity constructor (since you're inheriting the class, not implementing it)
Hope that helps...

Resources