Tuesday, November 29, 2011

Using the Dialog framework in SharePoint 2010 – Part 1


We are going to cover a new topic and a new series called dialog framework that has been introduced in SharePoint 2010 environment.

We all are aware about the dialogs if we have worked in web technologies. A model window that pops up and you can perform some operation without navigating to other page and then come back to the previous page.

If you would like to have a quick look at this series and would like to understand it as an overview, you can have a look at Opening any page as model dialog post.

Model dialog allows you to perform an operation and then close the window after performing an operation and you are back on the same page.

Most of the functionality is handled by two basic classes. SP.UI.Dialog and SP.UI.ModelDialog.
Classic example of model dialog is when you create, edit entries from list items, you get a dialog window to perform that. These pages open as a part of dialog.

We will deep dive into this dialog framework. In this post, I am going to cover a simple scenario of opening a window as a model dialog. You can have this enabled in your custom action click event, it can be a part of button click, and it can be a part of ribbon button.

Here is a very simple code written to open Bing search in model window from button click. We have defined a JavaScript that executes when we click on the button.

I’ve defined in a web part, one input type button and invoking a JavaScript function to open a model dialog.

<input type="button" value="Open Bing" onclick="OpenModalBing()" />


<script  type="text/javascript">

    function OpenModalBing()
    {
        var options = SP.UI.$create_DialogOptions();
        options.url = "http://www.bing.com";
        options.width = 750;
        options.height = 550;            
        SP.UI.ModalDialog.showModalDialog(options);
    } 
  
</script>

And this is what you get out of this code.



You can pass on different parameters in an option. So that when a window opens, it has some properties defined in it. just like how we do with window.open javascript function.

Here I have made some change to an existing code and look at the change in the behavior of how window open when button is clicked.

Now the maximize button does not appear as we have set it in JavaScript.






If you observe, the window opens in a center. If you would like to open a window at some other position, then you can use x and y properties and set it to options.



There is also another way you can create dialog. Observe the difference between two methods. Earlier we have created a window and then we set the properties on the window object with = sign. This is the other way around. Feel free to use the best suited way for you.

<script  type="text/javascript">

    function OpenModalBing()
    {
        var options = {
            url: 'http://www.bing.com',
            tite: 'Bing Search',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
           
        };
        SP.UI.ModalDialog.showModalDialog(options); 
    }  
   
</script>


Both give you the same result. Keep reading Part-2 of the series to know more about model dialog.

Wednesday, November 23, 2011

Cannot find save site as a template under site actions


I recently ran into a need of saving site as a template and then reuse it. However even though being site collection administrator, I could not find save site as a template option under site actions menu.



I was wondering why. Then I did some search and came to know that if your publishing feature was turned on. I went to the site features and turned off the publishing feature for a while.



Came back to the screen of site settings and yes, I could locate save site as a template.



Not sure, what is the logic behind this. However this is the trick that worked. After taking the site template I again turned on the publishing feature as it was necessary for the site.



Monday, November 21, 2011

Open different tabs in document library


Many times we want to open a document library in some or the other tabs default open. 


Sometime we want browse tab to open when we click on document library, sometime we want Documents tab and sometimes we might want library tab to open. 


To do this we need to set one parameter in the URL and that parameter is InitialTabId. Check out the following parameters set and observe the different tab that opens based on the InitialTabId set.







This is how we can set the different tabs to open by default when set that in URL.

Thursday, November 17, 2011

Metadata term store management - 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 talk about handling term store management programmatically. We will create term set and terms as well as navigate through the group inside term store session.

Before starting programming against term set, we need to take the term store session into a memory to work on it.

Here is a classic simple sample that helps you how to create a term set and add terms to it.

I have written a code in a button click event, in your case it can be at some different place based on your business logic.  Different actions are defined in the regions.

protected void btnAddTermStore_Click(Object sender, EventArgs e)
        {
            SPSite currentsite = SPContext.Current.Site;
          
                TaxonomySession session = new TaxonomySession(currentsite);

                if (session.TermStores.Count != 0)
                {
                    var termStore = session.TermStores["Managed Metadata Service"];

                    #region "Creating a group"

                    Group grpCloud = termStore.CreateGroup("Cloud Computing");
                    termStore.CommitAll();

                    #endregion

                     GroupCollection groupcoll = termStore.Groups;

                        foreach(Group grp in groupcoll)
                        {
                            if(grp.Name =="Sample Group")
                            {
                                 var termSet = grp.TermSets["Microsoft"];

                                 #region "Creating a term set"

                                 TermSet CTL = grp.CreateTermSet("Client Technologies");

                                 termStore.CommitAll();

                                 #endregion
                                                              

                                 #region "Adding Terms to an existing term set"

                                 Guid id = new Guid("{9CB722E2-D120-4A85-9740-447E01697CAC}");

                                 int lcid = CultureInfo.CurrentCulture.LCID;

                                 termSet.CreateTerm("TFS", lcid, id);

                                 termStore.CommitAll();

                                 #endregion
                            
                            }
                        }                
            }      
        }

All we have done here is we have taken the reference of taxonomy session of the site and then located term store management.


And this is what we get as a result



Once we have them in our hand, we created group. We then navigated to a group called Sample Group and located Microsoft Term Set. We also created a new term set called Client Technologies. We added new Term called TFS under Microsoft Term Set.

I hope this will give you an idea on basic manipulation of term store management.








Tuesday, November 8, 2011

Metadata term store management - Part-4


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

In addition to categorizing items in SharePoint 2010, we can also allow users to select specific tags for list items that they create.

When we allow users to select tags for specific list items, they become available for sorting and searching which allows organization to look for certain information faster.

Let’s check this out by playing with list items. Go to list settings and create one column. Call it as Keyword



Make sure that if you select allow multiple values, then you will not be able to sort that column in view.

I’ve kept it single selection for term.

You can select the display format as well. Would you like to see entire path of the term or directly the term in Display Format setting. After that we can select the specific term set to define. That means we allow user only to select terms from that term set only and no other terms allowed. I’ve allowed only terms from Microsoft Term set that we’ve defined in earlier post. If you want to create custom term set, you can create there using custom term option.


You can also allow fill in option. Only open term set allows user to submit new terms to a term set. If the term set is closed, then even by setting this option will not allow user to submit the term to a term store.



If you click on the icon, you get a model screen to select term.


And when you want to sort and filter with terms in list view, you get options.


Hope this helps. Read Part-5 for further reading.

Wednesday, November 2, 2011

Trying to use an SPWeb object that has been closed or disposed and is no longer valid

I recently faced this error while i was working on some object model and by looking into the code i realized that i did a little mistake.

This error itself states everything. I used the current site object in the using clause while performing some operation. we should never close the SPContext.Current.Site when we deal with any coding. If we have manually declared any site or web object, then only we have to dispose them.

I made this correction and my code worked well.

I hope this helps others not to repeat the same mistake again.

Tuesday, November 1, 2011

Anonymous access in SharePoint 2010 Survey


Filling up survey form in SharePoint is one of the common tasks that we perform. However when we want to capture a feedback in terms of survey but we would not like to know the name of a person who responded to survey then anonymous survey comes into the picture.

We directly cannot create a SharePoint 2010 anonymous survey. First we need to configure the site to have an anonymous site and then we can configure survey list to allow anonymous access.

First we need to make a setting in central administration. Go to Manage web applications->Select your specific web app->Click on zone type Default->then check the enable anonymous access.

Here are these steps shown as screen shots.






Now select anonymous policy from the ribbon and select None.



Now open your site to enable anonymous access. You can see this option only when you have set the anonymous option from the central administration.



You need to set where you would want to enable the anonymous access.


I’ve selected lists and library because we want to create an anonymous survey.

Open your survey list where you want to configure anonymous survey. First step that needs to be performed is to break the inheritance from the parent site. Without this step, we cannot configure anonymous survey.

Go to list settings and then permissions for a list and then click on anonymous access. This model page allows you to configure the action that anonymous user can perform. However you might see all options grayed out.





The reason behind this is strange, however these is a work around to this. Go to advance settings option of the list and then select read all responses.




Now again go back to the permission and then select anonymous access. You should be good to go for setting up the permissions.





Now the problem is we do not want other people to see other’s responses although created by field is always empty when we enable the anonymous survey, but still we do not want any person to read any other person’s response.

So go back to advance settings and now change from real all responses to read responses that were created by user.

And there you go, give link URL to respond to anyone and they should be able to respond to a survey. At the same time created by field will always be blank so that we never come to know who responded to survey.



As you can see I have three responses but who responded cannot be known.
















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