Social Icons

twitter google plus linkedin rss feed

Pages

17.9.10

Lambdas, Method Extensions and RunWithElevatedPrivileges

Nowadays it's not a problem for anyone to create a new site as an administrator regardless of the loged user, but I'd like to suggest a function that seems useful to me:
/// 
        /// Returns a NEW site with full permissions
        /// 
        public static SPSite OpenSiteWithElevatedPrivileges(this SPSite site)
        {
            SPSite newSite = null;

            SPSecurity.RunWithElevatedPrivileges(() => newSite = new SPSite(site.ID));

            return newSite;
        }
Looks easy, doesn't it? I like doing
SPSite site = SPContext.Current.Site.OpenSiteWithElevatedPrivileges();
What do you think?

No comments:

Post a Comment

15.9.10

Microsoft Chart Controls and MOSS2007

I've known the existence of this controls for long, but I've never had the urge to use them. A couple of times I've needed a graph I've created it drawing dots in a bitmap (obviously the simpler way)
But now at Manigent we have to deliver a lot of complex charts... I thought I could use Excel Services but it does not make sense to export data from the sharepoint object model to an Excel book and create the graph from there so I decided it would be a good chance to learn how to use the .Net charting controls.

First of all I followed the steps on this blog to set up the development environment:
Of course I didn't read the instructions and I went straight to the creation of the webpart.
First thing I did was to add the reference to the System.Web.DataVisualization DLL which was installed in C:\Program Files\Microsoft Chart Controls\Assemblies\

That's enough for you to start creating the webpart with Visual Studio. The code I used for the webpart was an adaptation of an example from the documentation:
public class ControlAssessmentChart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Chart Chart1 = new Chart();

        protected override void CreateChildControls()
        {
            Chart1.Width = 400;
            Chart1.Height = 300;
            Chart1.RenderType = RenderType.ImageTag;

            Chart1.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("StratEx Testing Chart", Docking.Top,
                new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold)
                , System.Drawing.Color.FromArgb(26, 59, 105));
            Chart1.Titles.Add(t);
            Chart1.ChartAreas.Add("Test 1");

            // create a couple of Test
            Chart1.Series.Add("Test 1");
            Chart1.Series.Add("Test 2");

            //ChartType can also be added to the series
            // add points to Test 1
            Chart1.Series["Test 1"].Points.AddY(5);
            Chart1.Series["Test 1"].Points.AddY(8);
            Chart1.Series["Test 1"].Points.AddY(12);
            Chart1.Series["Test 1"].Points.AddY(6);
            Chart1.Series["Test 1"].Points.AddY(9);
            Chart1.Series["Test 1"].Points.AddY(4);

            // add points to Test 2
            Chart1.Series["Test 2"].Points.AddY(2);
            Chart1.Series["Test 2"].Points.AddY(6);
            Chart1.Series["Test 2"].Points.AddY(18);
            Chart1.Series["Test 2"].Points.AddY(16);
            Chart1.Series["Test 2"].Points.AddY(21);
            Chart1.Series["Test 2"].Points.AddY(14);

            Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            Chart1.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
            Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            Chart1.BorderWidth = 2;

            Chart1.Legends.Add("Legend1");

            Chart1.Legends["Legend1"].Enabled = true;

            Controls.Add(Chart1);
        }
    }
After deploying the webpart in sharepoint I tried to run it but I got the error:

[HttpException (0x80004005): Error executing child request for ChartImg.axd.]

To solve it I followed the tip at this forum and added the line:
<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
To the web.config (inside <system.web><httpHandlers>)
After that I tried to run the webpart again but I got:

[DirectoryNotFoundException: Invalid temp directory in chart handler configuration [c:\TempImageFiles\].]

To solve this new issue I had to follow the instructions at this post and added:
<add key="ChartImageHandler" value="storage=file;timeout=20;" />
In the web.config inside <appSettings>

Finally it worked!.

RadarStackedBarStackedColumn
These charts belong to the same data set (is not the same in the code) but changing the ChartType parameter in the series.

It looks really powerful, let's see how it performs in production…

No comments:

Post a Comment

14.9.10

Developing is Painful in English Too

Why creating a second blog in English just to say the same stupid things that no one is interested in?

Easy:
  • First of all, because is free.
  • Second, because I have a small but growing number of English speaking friends that are just as crazy as I am and they could be interested.
  • Third, because is a good excuse to practice my English.
  • Fourth, because I want to compare the impact of the post in both languages.
  • Fifth, because I want to demonstrate that there’s no need of knowing English to write a blog in English.
  • And sixth, never write a pentalogue if you’re not a parapsychologist or R.A. Salvatore.
For those of you that don’t know my other blog, I base my posts in the certainty that no knowledge is trivial, this allow me to write about really trivial topics that real developers already know, but in the other hand have proven to be really useful to me, because I tend to forget easily. Yes, this blog is a temple of selfishness.


I really hope this blog is of any use to anyone…

Welcome to Developing is Painful in English Too

No comments:

Post a Comment