tag:blogger.com,1999:blog-5530571219240546432023-06-21T06:44:27.166+02:00C# develop"Considerate la vostra semenza: fatti non foste a viver come bruti ma per seguir virtute e canoscenza" (Dante)Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-553057121924054643.post-46894950089845543672016-06-14T10:53:00.000+02:002016-06-14T11:00:41.068+02:00PowerShell: SecureString - Save password (encrypted) to file and read them back<link href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeDefault.min.css" rel="stylesheet" type="text/css"></link>
<link href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shCore.min.css" rel="stylesheet" type="text/css"></link>
<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shAutoloader.min.js" type="text/javascript" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushPowerShell.min.js" type="text/javascript">
</script>
<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
Very useful script when you want to save passwords but not leaving them as plain text
<br>
<script class="brush: powershell" type="syntaxhighlighter">
<![CDATA[
"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString > C:\temp\pwd.txt
Get-Content C:\temp\pwd.txt | ConvertTo-SecureString | ConvertFrom-SecureString
]]>
</script>
Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com1tag:blogger.com,1999:blog-553057121924054643.post-17009857556021728602014-08-27T15:37:00.001+02:002014-08-27T15:37:43.661+02:00Arduino: send command through C#<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
I recently started to get interested in Arduino.
<br>
Among the many projects that I made using the Starter kit, I have not found a good example on how to communicate with Arduino using C#.
The Arduino program send on the LCD strings received through the serial port.
<br>
(See Project 11 of Project Book of starter kit for more info)
<br>
<h3>Arduino Sketch</h3>
<br>
<script class="brush: jscript" type="syntaxhighlighter">
<![CDATA[
#include <LiquidCrystal.h>
#include<stdlib.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
}
void loop()
{
String serialRead = "";
if (Serial.available()) {
serialRead = Serial.readStringUntil('\n');
printLcd(0,serialRead);
}
}
void printLcd(int row, String value)
{
lcd.clear();
lcd.setCursor(0,row);
lcd.print(value);
}
]]>
</script>
<br>
<br>
<h3>C# code</h3>
<br>
<script class="brush: jscript" type="syntaxhighlighter">
<![CDATA[
using System.IO.Ports;
// [..]
using (SerialPort p = new SerialPort("COM6", 9600))
{
p.Open();
p.WriteLine("Hello World!");
p.Close();
}
]]>
</script>
Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-31263395097322089292014-04-21T12:52:00.000+02:002014-04-21T12:52:59.024+02:00GAC Paths<p>
Starting with the .NET Framework 4 the paths of GAC are changed, here a brief summary.
<br>
<br>
<b><u>.NET 2</u></b>
<br>
<blockquote>%windir%\assembly</blockquote>
<br><br>
<b><u>.NET 4</u></b>
<br>
<blockquote>%windir%\Microsoft.NET\assembly. </blockquote>
<br>
<br>
</p>
<a href="http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.110).aspx">MSDN</a>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-55783118516651676902014-04-19T18:24:00.002+02:002014-04-19T18:24:32.041+02:00Sharepoint feature list instance<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
<p>
When you add more than one list to a feature we need to change the Type and the TemplateType from elements.xml files,
otherwise the second list get the first template.
<br>
<br>
In this example I haved changed the default to 10001
<br>
<br>
<script class="brush: xml" type="syntaxhighlighter">
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List project item, an error will occur when the project is run. -->
<ListTemplate
Name="MyName"
Type="10001"
BaseType="0"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="410"
DisplayName="My List Template"
Description="My Description"
Hidden="TRUE"
Image="/_layouts/15/images/itgen.png"/>
</Elements>
]]>
</script>
and the other file
<br>
<br>
<script class="brush: xml" type="syntaxhighlighter">
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance Title="My Title" OnQuickLaunch="FALSE" TemplateType="10001" Url="Lists/MyList" Description="My Description"></ListInstance>
</Elements>
]]>
</script>
<br>
<br>
Enjoy!
</p>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-50136345412043753692014-04-09T18:06:00.000+02:002014-04-09T18:07:09.427+02:00Linq to Sharepoint 2010 RunWithElevatedPrivileges don't work<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
Execution of Sharepoint Linq code with elevated privilege didn't work because Linq classes internally use the current context.
I has written this simple method to override this behavior.
<br />
<br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public static class ContextLinqSwitch
{
public static void RunWithElevatedPrivilegesEx(SPSecurity.CodeToRunElevated secureCode)
{
HttpContext backupCtx = HttpContext.Current;
try
{
HttpContext.Current = null;
SPSecurity.RunWithElevatedPrivileges(secureCode);
}
finally
{
if (HttpContext.Current == null && backupCtx != null)
{
HttpContext.Current = backupCtx;
}
}
}
}
]]>
</script>
<br />
<br />
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-46531825235119150752014-04-09T17:51:00.000+02:002014-04-12T17:01:40.692+02:00app.config for Office Addin<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
<p>
When you are developing a plugin office our settings should be saved in the app.config, but unfortunately this results in Excel.exe.config, Winword.exe.config, etc..
<br>
<br>
To solve this problem simply create a new .config file with the following name: <br>
<b><pluginname>.dll</b>
<br>
<br>
Visual Studio will create a new file called <b><pluginname>.dll.config</b> and this is what our plugin will read, as evidenced by the following statement
<br>
<br>
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
]]>
</script>
<br>
<br>
Remember to set up file property to "Copy to output directory"
<br>
<br>
See <a href="http://msdn.microsoft.com/en-IN/library/16e74h9c%28v=vs.120%29.aspx">MSDN page</a>
</p>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-63463801443232188692013-06-17T11:37:00.001+02:002013-06-17T11:37:39.426+02:00ASP.NET send/read JSON array via HiddenField<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
To send a JSON array in a postback, via hidden field, you can write this simple function.
<script class="brush: jscript" type="syntaxhighlighter">
<![CDATA[
var list = new Array();
list.push({ "ID": 1, "Title": "Title 1" });
list.push({ "ID": 1, "Title": "Title 2" });
$("#hiddenField").val(JSON.stringify(list));
]]>
</script>
On the postback event you can read your HiddenField like this:
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
JavaScriptSerializer js = new JavaScriptSerializer();
GridResponse[] list =
js.Deserialize<GridResponse[]>(hiddenField.Value);
]]>
</script>
Remember to reference the <b>System.Web.Extensions.dll</b>.
<br>
GridResponse class is declared as same structure of the JSON:
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public class GridResponse
{
public int ID {get; set;}
public string Title {get; set;}
}
]]>
</script>
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com2tag:blogger.com,1999:blog-553057121924054643.post-3970793688412654482013-03-11T17:48:00.001+01:002013-03-11T17:48:44.192+01:00ASP.NET WebForm ViewBag Property<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
Recently I developed some classic Asp.Net 3.5 webform pages, I need to use something like ViewBag present in Asp.NET MVC. For this reason I developed this small piece of code that is based on the ViewState.
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
protected object ViewBag
{
private set
{
this.ViewState["ViewBag"] = value;
}
get
{
if (this.ViewState["ViewBag"] != null)
{
return this.ViewState["ViewBag"];
}
return null;
}
}
]]>
</script>
This code must be written in our ASP.NET page; after that should be set with a simple assignment.
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
TestClass t = new TestClass();
t.Id = 1;
t.Description = "My Description";
this.ViewBag = t;
]]>
</script>
Obviously the TestClass must be serializable;
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
[Serializable]
public class Test
{
public int Id { get; set; }
public string Description { get; set; }
}
]]>
</script>
In the markup of my page, I use the properties as follows:
<script class="brush: xml" type="syntaxhighlighter">
<![CDATA[
<!-- Import for TestClass namespace -->
<%@ Import Namespace="MyApp" %>
<span>Description:<%= ((TestClass)this.ViewBag).Description %></span>
]]>
</script>
<br>
Enjoy!
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-76432832554268600682013-02-01T12:25:00.000+01:002013-02-01T12:25:35.283+01:00UAC - Run as Administrator your C# Application<style>
div.body
{
font-family: Verdana, sans-serif;
}
</style>
<br />
<div class="body">
If you need to run your .NET application as Administrator with Visual Studio is very easy.
<br />
<ul>
<li>Add new item of type app.manifest to your application</li>
<li>Open file and change
<br />
<br />
<span style="color: blue; font-family: Consolas; font-size: 13px;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 13px;">requestedExecutionLevel</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> </span><span style="color: red; font-family: Consolas; font-size: 13px;">level</span><span style="color: blue; font-family: Consolas; font-size: 13px;">=</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;">asInvoker</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> </span><span style="color: red; font-family: Consolas; font-size: 13px;">uiAccess</span><span style="color: blue; font-family: Consolas; font-size: 13px;">=</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;">false</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> /></span><pre style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 13px;"></pre>
<br />
with
<br />
<br />
<span style="color: blue; font-family: Consolas; font-size: 13px;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 13px;">requestedExecutionLevel</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> </span><span style="color: red; font-family: Consolas; font-size: 13px;">level</span><span style="color: blue; font-family: Consolas; font-size: 13px;">=</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;">requireAdministrator</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> </span><span style="color: red; font-family: Consolas; font-size: 13px;">uiAccess</span><span style="color: blue; font-family: Consolas; font-size: 13px;">=</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;">false</span><span style="background-color: white; font-family: Consolas; font-size: 13px;">"</span><span style="color: blue; font-family: Consolas; font-size: 13px;"> /></span><pre style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 13px;"></pre>
</li>
<li>Save and build</li>
</ul>
Enjoy!
<br />
<div>
<br /></div>
</div>
Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-74836519766359409692012-06-15T20:34:00.000+02:002013-03-11T17:55:49.945+01:00Sharepoint EventCache and EventLog tables are too big<style>
.body
{
font-name: verdana;
}
</style>
<h3> When the Sharepoint database is out of control !</h3>
<br/>
A few days ago I noticed that the database for my WebApplication Sharepoint was overgrown. On opening the database I noticed that 90% of space was concentrated in just two tables: <b>EventCache</b> and <b>EventLog</b>.
<br/>
<br/>
To save space, at least for the EventCache table, I saw that you must set the <i>throttling Changelog</i>; to do this select from Central Admin <b>WebApplication -> General Settings -> Resource Throttling</b>, and set the <b>Change Log</b> to a low number (eg . 5 days).
<br/>
<br/>
Once this is done run the job called <b>Change Log</b> which is responsible for deleting records from the table <b>EventCache</b> (despite the name ...).
<br/>
<br/>
Once the job have finished, if you are not satisfied of the result or the disk space occupied is mainly assigned to the SQL log file (the file. Ldf), then you can do this:
<br/>
<br/>
1. Open the SQL managment console.<br/>
2. Select the database of the WebApplication.<br/>
2. Set the database recovery mode to Simple.<br/>
3. Run the Shrink Database.<br/>
4. Reset to Full recovery mode.<br/>
<br/>
For the implications regarding the Recovery Mode SQL I recommend you read it
<a href="http://msdn.microsoft.com/en-us/library/aa173531 (v = SQL.80).aspx">here</a>
<br>
<br>
<b>Warning:</b> Most of the time is often a program that update continuously items in the Sharepoint list, check if there are some custom Job Timer, Service or other programs.
Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com2tag:blogger.com,1999:blog-553057121924054643.post-30365152412935096142012-06-01T16:28:00.001+02:002012-06-01T16:55:55.916+02:00Report Builder - List does not exist - 0x82000006<style type="text/css">
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
Today I encoutered a strange error while developing a Sharepoint list report with Report Builder.
<br />
<br />
<code>
List does not exist.
<br />
The page you selected contains a list that does not exist. It may have been deleted by another user.
<br />
0x82000006 (Microsoft SQL Server Report Builder)
<br />
</code>
<br />
<br />
This error happens when you design a new dataset, select your list and click <b>Run Query</b>.
<br />
After some troubleshooting I found the solution:
<br />
Into the Query Designer, after you have selected the Fields, click <b>Edit as Text</b> then in the <b>command type</b> select <b>Text</b> then re-click <b>Edit as Text</b>, to return to the graphic designer.
Finally click <b>Run Query</b>
<br />Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com1tag:blogger.com,1999:blog-553057121924054643.post-91564600463919247762012-03-26T10:34:00.002+02:002012-03-30T23:13:26.886+02:00<nativehr>0x80070057</nativehr><nativestack></nativestack> ArgumentException<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
If you encounter an error like this:
<br/>
<br/>
<b><blockquote><nativehr>0x80070057</nativehr><nativestack></nativestack> ArgumentException </blockquote></b>
<br/>
<br/>
While developing query with Linq To Sharepoint, most probably you have a wrong target platform; you must target ALL your project to x64 platform.Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-52822376769487993912012-03-12T12:07:00.000+01:002012-03-16T18:09:29.874+01:00Sharepoint 2010 DataFormWebPart doesn't show all the items<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript">
</script>
<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
Some time ago I developed a custom page with a DataFormWebPart paged and groping enabled; with my surprise I found that clicking on "next page" icon a javascript error was raised on the javascript console.
<br/>
<br/>
After some investigation I discovered that the javascript embedded into the <a> element was malformed.
<br/>
<br/>
The charter <b>'</b> was correctly encoded %27 in the source html but when redered by the browser and then decoded the javascript function fail.
<br/>
<br/>
The code below is a simple workaround for the bug
<br/>
<br/>
<script class="brush: jscript" type="syntaxhighlighter">
<![CDATA[
<script src="script/jquery-1.7.1.min.js" type="text/javascript" ></script>
<script>
$(document).ready(function()
{
$("a[href^='javascript: __doPostBack']").each(function(i)
{
var hr = $(this).attr("href");
$(this).attr("href", hr.replace("%27","%5C%27"));
});
});
</script>
]]>
</script>
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-60928588229454799632012-02-29T19:15:00.000+01:002012-02-29T19:15:55.679+01:00Expected hex 0x in '{0xdddddddd, etc}<style>
body
{
font-name: verdana;
{
</style>
Sometime, developing a Sharepoint custom page with xslt view and selecting a row, results is this javascript error:<br />
<br />
<code>Expected hex 0x in '{0xdddddddd, etc}</code>
<br />
<br />
Apparently there are some missing/malformed GUID into the xslt control.<br />
Below the step necessary to resolve this issue:<br />
<br />
1. Grab the guid of the AllItems.aspx page of the linked datasource list<br />
2. Open the page and select the xslt view, add a new property named ViewGuid and set with the previous value<br />
3. Change the value of the property Name of the XmlDefinition node with the previous value.<br />
<br />
Side Effects:<br />
If you have removed link for add new element this is restored.<br />
<br />Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com3tag:blogger.com,1999:blog-553057121924054643.post-85767008395422248322012-02-24T12:34:00.000+01:002013-03-13T13:10:38.192+01:00Read Sharepoint Lookup field<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
Sharepoint stores the informations in Lookup fields this way:<br />
<br />
<code>ID;#VALUE</code>
<br />
<br />
Sometimes we need read just the value of id. Someone takes the field value as a string and split the content using <code>Split</code> or <code>IndexOf</code>. This technique is valid but..., if Microsoft changes the way the lookup information are saved ?<br />
<br />
To read a lookup field you can use <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvalue.aspx">SPFieldLookupValue</a><br />
<br />
Using SPFieldLookupValue we can split the values of the lookup field in a simpler way
<br />
<br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
string rawvalue = item["FieldName"].ToString();
SPFieldLookupValue fieldValue = new SPFieldLookupValue(rawvalue);
// Contain the value
fieldValue.LookupValue
]]>
</script>
<br />
<br />
Enjoy!
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-68517522397152031222012-02-22T13:34:00.001+01:002012-02-26T12:28:40.472+01:00Sharepoint ConnectionString from EventHandler<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
In some cases, developing a Sharepoint Event Handler, we needs to read the web.config; the code below solves the problem
<br />
<br />
<br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public string GetConnectionString(SPItemEventProperties properties)
{
using (SPSite siteCollection = new SPSite(properties.SiteId))
{
if (!string.IsNullOrEmpty(siteCollection.WebApplication.Name))
{
string webApplicationName = siteCollection.WebApplication.Name;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplicationName);
return config.ConnectionStrings.ConnectionStrings["MyConnection"].ConnectionString;
}
}
return null;
}
]]>
</script>
This is the link to my <a href="http://www.istante.info/blog/post/Leggere-le-connectionString-del-webconfig-da-Event-Receiver-di-Sharepoint-2010.aspx">Old Post</a>
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-9067359156099124802012-02-22T10:19:00.000+01:002012-02-28T14:56:01.491+01:00Query Sharepoint 2010 list via Javascript<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript">
</script>
<style>
body
{
font-family: Verdana;
}
code
{
color: navy;
}
</style>
<h4>Query Sharepoint 2010 list using JavaScript and Client ObjectModel</h4>
<br />
To read items from a Sharepoint list within a custom page you can use Javascript and client object model, below I explain necessary step to achive this.
<br />
<br />
Add a new page and reference the Object Model on the content of the page (Sp.js), I add also a reference to the jQuery library for the document ready event.
<br />
<script class="brush: xml" type="syntaxhighlighter">
<![CDATA[
<scriptlink Name="SP.js" runat="server" OnDemand="true"
Localizable="false" />
<script src="script/jquery-1.7.1.min.js" type="text/javascript" ></script>
]]>
</script>
<br />
One very important thing is that you give the page time to load the object model javascript, to do this in the document ready function I call <code>executeOrDelayUntilScriptLoaded</code> that can wait for the fully loaded js.
<script class="brush: jscript" type="syntaxhighlighter">
<![CDATA[
var product;
function docReady()
{
SP.SOD.executeOrDelayUntilScriptLoaded(getValues,'SP.js');
}
function getValues()
{
try
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('ListName');
this.product = list.getItemById(1);
context.load(product, 'Col1', 'Col2');
context.executeQueryAsync(Function.createDelegate(
this,
this.productReceived),
Function.createDelegate(this, this.failed)
);
}
catch (e)
{
alert(e);
}
}
function productReceived()
{
var value = this.product.get_item("ColName");
alert(value);
}
function failed(sender, args) {
alert('failed. Message:' + args.get_message());
}
$(document).ready(docReady());
]]>
</script>
<br />
<br />
The code I think is pretty clear: in the document ready I call
<code>executeOrDelayUntilScriptLoaded</code> which waits for the loading of object model javascript, and after I calls the function <code>getValues</code>.
<br />
<br />
Function <code>getValues</code> create the sharepoint object model context and query the specified list for item with ID = 1. To the <b>asynchronous</b> function <code>executeQueryAsync</code> you must specify two callback: the first for the success, and the other for the errors.
<br />
<br />
If the call return without errors you must get values with the function <code>get_item("ColName")</code>.
<br />
Enjoy!
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0tag:blogger.com,1999:blog-553057121924054643.post-9286091387757158082012-02-09T12:29:00.000+01:002013-03-04T13:10:59.028+01:00ASP.NET strongly typed querystring parameters<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript">
</script>
<style>
body
{
font-family: verdana;
}
</style>
To read strongly typed querystring parameters, I wrote a simple extension method that does the "dirty" job conversion
<br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
public static T GetParam<T>(this HttpRequest value, string name)
{
if (value.QueryString.AllKeys.Any(q => q == name))
{
object val = value.QueryString[name];
try
{
return (T)Convert.ChangeType(val, typeof(T));
}
catch
{
}
}
return default(T);
}
]]>
</script>
The use is simple:
<br />
<script class="brush: csharp" type="syntaxhighlighter">
<![CDATA[
var value = this.Request.GetParam<tipo>("MyParameter");
]]>
</script>
<br /><script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com1tag:blogger.com,1999:blog-553057121924054643.post-78909907781347764062012-02-04T17:16:00.000+01:002012-02-24T11:56:46.418+01:00Open Sharepoint editing popup via javascript<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript">
</script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript">
</script>
<style>
body
{
font-family: verdana;
}
</style>
In some cases, developing within sharepoint 2010, you need to launch through javascript a editing popup for an item. To achieve this functionality you must enter this code snippet:
<br />
<script class="brush: js" type="syntaxhighlighter">
<![CDATA[
//Handle the DialogCallback callback
function DialogCallback(dialogResult, returnValue) {
window.location.reload();
}
//Open the Dialog
function openEditDialog(url) {
var options = {
url: url,
width: 700,
height: 700,
dialogReturnValueCallback: DialogCallback
};
SP.UI.ModalDialog.showModalDialog(options);
}
]]>
</script>
The url passed to the function must be according to this scheme
<br />
<a href="http://www.blogger.com/blogger.g?blogID=553057121924054643#">http://localhost/<miosito>/Lists/<nomelista>/EditForm.aspx?ID=<id>&IsDlg=1 </nomelista></a>
<br />
Replacing <b><mysite></b> with link to sharepoint site, or directly with
<br />
<a href="http://www.blogger.com/blogger.g?blogID=553057121924054643#">/Lists/<listname>/EditForm.aspx?ID=<id>&IsDlg=1</listname></a>
<br />
Replace <b><id></b> with the id of the element
Enjoy!
<script type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>Massimohttp://www.blogger.com/profile/00349249883590280296noreply@blogger.com0