C# razorengine for templates how to write if statement - razorengine

https://github.com/Antaris/RazorEngine
I am using this RazorEngine to generate emails like:
Hi {%=Firstname%} {%=Lastname%},
now I would like to check if Firstname and Lastname are not empty
I try like:
#if (Firstname != '' || Lastname != '') { Hi, } else {
Hi{%=Firstname%} {%=Lastname%}, }
but does not work. if statement is printed as HTML

Related

Not able to get Lookup field value in Sharepoint 2013 online Custom List Using SPD

I have one lookup column named "ActivityStatus" and it has two values.
and I just want to check that lookup field value on Presave Action.
Below is my code but it is not working.
<script type="text/javascript">
function PreSaveAction()
{
alert("Inside");
var elm = document.getElementById("ActivityStatus");
alert(elm);
if (elm == "Incomplete" || elm == "Inprogress")
{
document.getElementById("ActivityStatus").style.display='none';
alert("Previous Activity is in Progress...");
return false ;
}
else
{
return true ;
}
}
</script>
The way you are getting lookup value will not work.
you have to do something as below. As it is rendered as a Select tag.
Refer below code :
function PreSaveAction()
{
var elm = document.getElementById("plpace ID your control Here");
var elmValue = elm.options[elm.elmectedIndex].text.trim();
if (elmValue == "Incomplete" || elmValue == "Inprogress")
{
//
// your other code
//
}
else
{
return true ;
}
}
Here in variable selectedValue you will get actual value.
And replace the ID accordingly for your control..

String comparison not working for sharepoint multiline text values

I am fetching data from sharepoint list for a multi line column.
And then split the data by space and comparing it to other string but despite the value in both the strings being same it gives false result.
Please follow the below code:
string[] strBodys = SPHttpUtility.ConvertSimpleHtmlToText(Convert.ToString(workflowProperties.ListItem[SCMSConstants.lstfldBody]), Convert.ToString(workflowProperties.ListItem[SCMSConstants.lstfldBody]).Length).Split(' ');
bool hasKwrdInBody = false;
foreach (SPItem oItem in oColl)
{//get all the keywords
string[] strkeyWrds = SPHttpUtility.ConvertSimpleHtmlToText(Convert.ToString(oItem[SCMSConstants.lstfldKWConfigKeywordsIntrName]), Convert.ToString(oItem[SCMSConstants.lstfldKWConfigKeywordsIntrName]).Length).Split(',');
//in body
foreach (string strKW in strkeyWrds)
{
string KWValue = strKW.Trim(' ').ToLower();
foreach (string strBdy in strBodys)
{
string BodyValue = strBdy.Trim(' ').ToLower();
//if (strKW.ToLower().Equals(strBdy.ToLower()))
if(KWValue == BodyValue) //here it always gives false result
{
hasKwrdInBody = true;
break;
}
}
if (hasKwrdInBody)
break;
}
if (!hasKwrdInSbjct && !hasKwrdInBody)
{
continue;
}
else
{
//set business unit to current groups rule
bsnsUnitLookupFld = new SPFieldLookupValue(Convert.ToString(oItem[SCMSConstants.lstfldBsnsUnit]));
asgndTo = new SPFieldUserValue(objWeb,Convert.ToString(oItem[SCMSConstants.lstfldKWConfigAssignedToIntrName])).User;
groupName = Convert.ToString(oItem[SCMSConstants.lstfldKWConfigAssignedToGroupIntrName]).Split('#').Last();
break;
}
}
Please mind that i am trying to get multi line text from sharepoint list
Please provide your suggestions.
That also depends on the exact type of your Multiline field (e.g Plain Text or RichText, etc.).
Maybe it would be clear if you just added some logging writing out the values you are comparing.
For details on how to get the value of a Multiline textfield check Accessing Multiple line of text programmatically
and here for RichText
I got it working by comparing and counting the characters in both the strings. Actually some UTC codes were embedded in to the string. First I removed those characters using regular expression and then compared them and it worked like a charm.
Here is the code snippet, might help some one.
string[] strBodys = SPHttpUtility.ConvertSimpleHtmlToText(Convert.ToString(workflowProperties.ListItem[SCMSConstants.lstfldBody]), Convert.ToString(workflowProperties.ListItem[SCMSConstants.lstfldBody]).Length).Split(' ');
bool hasKwrdInBody = false;
foreach (SPItem oItem in oColl)
{//get all the keywords
string[] strkeyWrds = SPHttpUtility.ConvertSimpleHtmlToText(Convert.ToString(oItem[SCMSConstants.lstfldKWConfigKeywordsIntrName]), Convert.ToString(oItem[SCMSConstants.lstfldKWConfigKeywordsIntrName]).Length).Split(',');
//in body
foreach (string strKW in strkeyWrds)
{
string KWValue = strKW.Trim(' ').ToLower();
KWValue = Regex.Replace(KWValue, #"[^\u0000-\u007F]", string.Empty); //here replaced the utc codes
foreach (string strBdy in strBodys)
{
string BodyValue = strBdy.Trim(' ').ToLower();
BodyValue = Regex.Replace(BodyValue, #"\t|\n|\r", string.Empty); // new code to replace utc code
BodyValue = Regex.Replace(BodyValue, #"[^\u0000-\u007F]", string.Empty); //new code to replace utc code
//if (strKW.ToLower().Equals(strBdy.ToLower()))
if(KWValue == BodyValue) //here it always gives false result
{
hasKwrdInBody = true;
break;
}
}
if (hasKwrdInBody)
break;
}
if (!hasKwrdInSbjct && !hasKwrdInBody)
{
continue;
}
else
{
//set business unit to current groups rule
bsnsUnitLookupFld = new SPFieldLookupValue(Convert.ToString(oItem[SCMSConstants.lstfldBsnsUnit]));
asgndTo = new SPFieldUserValue(objWeb,Convert.ToString(oItem[SCMSConstants.lstfldKWConfigAssignedToIntrName])).User;
groupName = Convert.ToString(oItem[SCMSConstants.lstfldKWConfigAssignedToGroupIntrName]).Split('#').Last();
break;
}
}

Session variable fine in one action, null in next

I have a simple application. When a user logs in it creates a session variable and redirects them to another controller that pull up some info from the database. I need to store this to a session variable then return it to the view. The initial part is fine, it returns it and I see the information. However, when I try to create a search query it returns that the session is null even though I never clear it.
public ActionResult ShowCourses()
{
if (Session["Username"] != null)
{
string Username = Session["Username"].ToString();
using (DefaultConnection db = new DefaultConnection())
{
var model = from c in db.Courses
where c.Username == Username
select c;
Session["Courses"] = model.ToList();
var Courses = Session["Courses"];
return View(Courses);
}
}
else
{
return RedirectToAction("Login", "Users");
}
}
But when I try to run a search query to loop through the session, it brings back that Courses is null. The problem is that coursesQuery returns null and I'm not sure why.
public ActionResult SearchCourses(string query)
{
if (Session["Username"] != null)
{
var coursesQuery = Session["Courses"] as IEnumerable<Course>;
if (coursesQuery != null)
{
// Do Something
}
}
}
You did a .ToList() on the course colleciton before setting to Session.
Try this. Use List<Course> when you read it back.
if (Session["Courses"] != null)
{
var coursesQuery = (List<Course>) Session["Courses"];
}

Get the User Id - SharePoint 2010

I have a column that has names of the user. The column type is Single line of Text and the user name is stored in the format LastName, FirstName;
I would like to get the user id and email address of the stored user name.
I tried
string fieldValue = item["ProjectManager"] as string;
SPFieldUserValueCollection users = new SPFieldUserValueCollection(item.Web, fieldValue);
if (users != null)
{
/* The users.count is always zero */
foreach (SPFieldUserValue user in users)
{
if (user.User != null)
{
}
}
}
I cannot change the column type to Person or Group and it would remain as Single Line of Text. Please let me know how can I achieve this . I have been trying this for couple of hours now.
Try this:
public SPUser GetUser(SPWeb web, string userstring)
{
var user = FetchUser(web, userstring);
if (user == null)
{
SPPrincipalInfo info = SPUtility.ResolvePrincipal(web, userstring, SPPrincipalType.User, SPPrincipalSource.All, null, false);
if (info!=null)
user = FetchUser(web, info.LoginName);
}
return user;
}
public SPUser FetchUser(SPWeb web, string userstring)
{
return web.AllUsers.Cast<SPUser>().FirstOrDefault(
i => i.Name == userstring || i.LoginName == userstring || i.Email == userstring);
}
From SPUser you get get the user ID.
Get SPUser in your case with:
var user = GetUser(item.Web,fieldValue )

Use other Part instead of TitlePart in menu

I have a menu that has alot of query links. That will generate a good menu with the content of the TitlePart as the text.
I would like to use another parts field, that I have made, instead of the text in the TitlePart. Only in the menu and not on the contents page.
Is there a possibility to modify some module in order to achive this or is there a placement I can use to solve the problem??
I solved the problem by inserting this
protected override void GetItemMetadata(GetContentItemMetadataContext context)
{
var part = context.ContentItem.As<ReferenceCompanyPart>();
if (part != null && part.CompanyName!= null && part.CompanyName != "") {
context.Metadata.DisplayText = part.CompanyName;
}
else {
var titlepart = context.ContentItem.As<ITitleAspect>();
context.Metadata.DisplayText = titlepart != null ? titlepart.Title : "";
}
}
Then my ReferenceCompanyName was used insted of the TitlePart in the menu

Resources