Friday, January 27, 2012

Masking fields in SharePoint 2010 list and libraries


I came across to a situation where people wanted to enter the list items and upload the documents but would not like see their names in them. They also did not want anonymous access to list or library.

So now what can be the solution? Then I read about the PowerShell and came to know one technique that we can imply to do this.

Here is a classic example. I have one customer list and after using following command in PowerShell, I got what I wanted.





And here is the result



You can also use the SharePoint object model to set the same property. Use SPList.ShowUser property to perform the same operation where SPList is your list object.

I hope you like this trick.




Monday, January 16, 2012

Sandbox Solutions in SharePoint 2010 – Part 4


If you have not gone through Part-1 to Part-3 of this series, I would recommend you reading them first and then continue from here on.

In this post, we are going to see how Visual Studio helps us creating SandBox solutions easily.
Let us open the same example that we wrote in our earlier post. 

Let us add a code of line which is actually not permitted in sandbox solution.

protected override void CreateChildControls()
        {
           
                Label lbl = new Label
                {
                    Text = "Hello from sandbox",
                    ID = "lblSandBox",
                    ForeColor = System.Drawing.Color.Blue
                };

                Controls.Add(lbl);
           
               

                SPSecurity.RunWithElevatedPrivileges(delegate
                {

                });
           
        }
       

We know that SPSecurity is the class which is not allowed in the sandbox solution. However when I build the project and even create a package, it allows me to do that.






No errors were given and let us see if we try to deploy this package and see what happens.


So how can we ensure that something which is not allowed in sandbox solution, we should not be able to use while program against sandbox solutions.

There is a way to do this, however we need to keep in mind that this should be done only on developer’s machine and not on production. I am showing this just to give you an idea that we have something like this available which we can use but not for the production.

We know that the Microsoft.SharePoint assembly which is there in the project refers to this global path.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll

Let us go ahead and locate one more Microsoft.SharePoint dll but at other location and add it to the project. Remove the Microsoft.SharePoint dll which is right not in the project.


C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\UserCode\assemblies\Microsoft.SharePoint.dll

And now try to build the project.










It does not allow us to build the project because SPSecurity is not allowed to be used inside sandbox solution. 

Do not forget to remove and get the original assembly back while deploying to production.

I hope you like this series of sandbox solutions. We will keep adding new content in SandBox solution when we come across to anything. 

Till then keep reading, keep visiting SharePoint Kings as we together explore the world of SharePoint.








Wednesday, January 11, 2012

Sandbox Solutions in SharePoint 2010 – Part 3


In this post we are going to see how resources count in sandbox solution. If you have not gone through part-1and Part-2 of this series, I would recommend you reading it first and then continue reading from here on.

Let us change the code that we had written in earlier post. This time we will write something which will utilize the resources and hence we get some bar increment in resources area in solutions.

Default resource quota allotted is 300 and we can change that using PowerShell or through central administration.

Let us do this with PowerShell as it is the easiest option. Open Powershell module from administrative tools.


Replace the URL of your site collection.

You need to be a farm administrator of course to perform all these operations.

So now if you come back and check the resource quota allocation then you should see it has dipped from 300 to 100.


Now let us add a code to our existing solution which will consume resources.

public class SandBoxWebPart : WebPart
    {
        protected override void CreateChildControls()
        {
           
                Label lbl = new Label
                {
                    Text = "Hello from sandbox",
                    ID = "lblSandBox",
                    ForeColor = System.Drawing.Color.Blue
                };

                Controls.Add(lbl);

                RunLoop();
           
        }

        private void RunLoop()
        {
            int i = 0;

            while (true)
            {
                i += 1;
            }
        }
    }


What we have done is we have placed a never ending function which keeps executing while true and hence some point of time it will exceed the run time environment threshold value and throw an error.

So let’s build the solution and then generate WSP and then upload to solution gallery. After deploying and activating this new WSP and coming back to home page where this web part is placed, here is what we get.


Because of infinite loop we get this error message, if we keep executing this code for number of times then we can see the change setting up on resource quota bar and usage increased in solution gallery.


Farm administrator can also block the solution if administrator found that particular solution is consuming many resources. Go to system settings, user code solutions.


Select wsp to block and click on block.



And this is what we get when we try to run our solution.




In next post we are going to explore visual studio experience and capability working on sandbox solutions. Read Part-4 for further reading.

Wednesday, January 4, 2012

Sandbox Solutions in SharePoint 2010 – Part 2


In this post we are going to see how to create and deploy sandbox solution. If you have not gone through part-1 of this series, I would recommend you reading it first and then continue reading from here on.

So let us go ahead and start the visual studio and this time we will create an empty project and select sandbox solution and not the farm solution. Select empty solution project.


Mention the URL of the site that you would like to connect to.

And click on finish. You will be presented with this screen.


If you see a property of a project, then you can see a property called Sandbox solution as true. If you wish to turn this solution to farm solution, you can always come back to property setting and set it to false. This is a handy tip. Isn’t it? J As of now we will keep it true as we are developing sandbox solution.


Now click on add new item and select web part (not visual web part – as it is not supported in sandbox solution). Give it a name SandBoxWebPart and add it to solution.

We are going to write a simple code to this. Write a code in createchildcontrols method.

protected override void CreateChildControls()
        {
            if (!Page.IsPostBack)
            {
                Label lbl = new Label
                {
                    Text = "Hello from sandbox",
                    ID = "lblSandBox",
                    ForeColor = System.Drawing.Color.Blue
                };

                Controls.Add(lbl);
            }
        }

So now we are going to package this solution, not deploy because we want to deploy it as a sandbox solution. So now you get a wsp created in your physical path of this project once you right click on project and select package.


And you can see your wsp file


Now the beauty is if you are the site collection administrator you can go ahead and deploy and activate this solution.

So let us open the site and go to site settings and go to gallery and click on solutions.


As you can see solutions quota has been assigned by farm administrators. As and when solution uses the resources it keeps increasing the bar which tells us the limit that solution has used.

Click on the solutions tab and then upload the solution. You can also down the solutions from Microsoft office.com and use it in your application.



Once you upload, you also get an option to activate at the same time. We will activate the solution. You can see it has been activated and now deployed.


Now go ahead and open the site, edit the page and add a web part. Look for the web part with the name ‘SandBoxWebPart’ in custom category.


Add that web part and this is what you get.


In next post, Read Part-3 to see how the resources count in sandbox solution. 

Tuesday, January 3, 2012

Wishing you all a very happy new year

Hello SharePointKings reader,

Wish you all a very happy new year and prosperous year ahead. Thank you all for being such a wonderful readers. We appreciate your reading and participating in polling. We value your feedback. We will continue to grow and expand our value of sharing the knowledge at its best.

Thank you once again. Keep visiting, sharing and reading SharePointKings.



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com