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.




Read More…

Wednesday, January 25, 2012

The result is out

Finally the result is out


Read More…

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.








Read More…

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.
Read More…

Friday, January 6, 2012

The Result is out

Finally the result is out


Read More…

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. 
Read More…

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.
Read More…

Thursday, December 22, 2011

Sandbox Solutions in SharePoint 2010 – Part 1

Before starting what sandbox solution is, first let us understand what solution mean? If you have worked with MOSS 2007 version then you must be aware of solutions. Solutions can be deployed over a farm. With 2010 version solution can also be deployed in sandbox. Solution may contain web parts, images, pages, event handlers, lists etc.


Sandbox solution is a new concept introduced in SharePoint 2010. Advantage of sandbox solution is this solution actually executes in a restricted zone. It cannot access everything in object model. It cannot access certain resources from SharePoint object model.

This is the advantage because it cannot affect the farm environment. If something goes wrong in the sandbox solution then entire farm does not get affected.

Sandbox solution is always given certain number of resource allocation space and that is monitored by farm administrator. If farm administrator wants, then it can be disabled or even it can also be promoted to be a farm solution as an upgrade.

Other advantage of sandbox solution is it can free farm administrator for deployment stand point. Site collection administrator can deploy and activate the sandbox solution. If the sandbox solution does not contain any assembly, then even a user with full control over a top level site of site collection can also deploy the solution.

Sandbox solution runs in a separate process and that is the reason if something goes wrong in sandbox solution then it does not impact farm. It does not run in w3wc process but runs under SPUCWorkerProcess.

For the hosted SharePoint environment, this can be one of the best ways to deploy and maintain the solutions and sites.

There are many limitations in terms of accessing resources while you program against sandbox solutions in SharePoint. I am listing down some of them. You cannot:

    1)    System.IO, ADO.Net
    2)    Write to disk files
    3)    Oher site collection content
    4)    SPSecurity.RunWithElevatedPrivileges
    5)    Change threading model
    6)    Create visual web part
    7)    Access programmatic workflow
    8)    Timer Job
    9)    Web Application scoped features 

Here are certain items that you can do in sandbox solution environment

   1)    Web Parts (not visual)
   2)    Event receivers
   3)    Feature receivers
   4)    Custom SharePoint Designer Workflow Activity

     There are certain steps that farm administrator has to perform before setting up sandbox solution environment if entire set up is farm architecture. Farm administrator starts the sandbox service in each server in farm, configures the best load balancing service for sandbox solutions on each server and then set the resource quotas.

Read Part-2 for further reading.
Read More…

Tuesday, December 20, 2011

The result is out

Hi


Finally the result is out. It seems people think that MS has to work harder when Google launches its OS.



Read More…

Friday, December 16, 2011

Using the Dialog framework in SharePoint 2010 – Part 5


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

In this post, we are going to see how we can pass parameters to model dialog window from a parent page.

We are going to take the same example which we had taken in previous posts.

Add a query string to a url parameter to the function.

    function OpenModalDialog() {        

        var sitename = 'SharePointkings';

        var options = {
            url: '/{site url}/_layouts/SharePointActivities/CustomDialog.aspx?sitename=' + sitename,
            tite: 'Open a custom dialog',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
             args:parameters,
            dialogReturnValueCallback: FunctionToCall
           
           
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

If you would like to get an intellisence of Visual studio for SP.UI and SP.UI.Debug javascript file, then add a reference like this

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />
</script>


And then when you type, you get the Intelisence.


And on the receiving model dialog page write down the following JavaScript functions in your model dialog page.

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />

   if (typeof (_spBodyOnLoadCalled) == 'undefined' || _spBodyOnLoadCalled) {  
   
   window.setTimeout(AlertParametersPassedFromParent, 0);       
     }  
   else {

       _spBodyOnLoadFunctionNames.push("AlertParametersPassedFromParent");

       }

 function getQuerystring(key, default_) {
     if (default_ == null) default_ = "";
     key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
     var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
     var qs = regex.exec(window.location.href);
     if (qs == null)
         return default_;
     else
         return qs[1];
 }


 function AlertParametersPassedFromParent() {   

     var sitename = getQuerystring('sitename');

     alert(sitename);
   }
   

  </script>


When the page loads, we push the function AlertParametersPassedFromParent to execute and called a function called getQueryString which is actually logic to retrieve query string which is passed from the parent page. There is no ready method in JavaScript like server side methods. So we have to use our own method to get that.

And we have then just prompted the parameter value with alert to see if it has passed properly.

Here is what we get as an end result.




I hope you have enjoyed the entire series of model dialog framework. We will try and add some new content if we find anything interesting about model dialog more. Of course we encourage and appreciate our readers to share their experiences so that we all SharePoint working community get benefited from it.





Read More…



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