Friday, 10 January 2014

Programmatically Hiding the "Overwrite existing files" checkbox for a single library,, Solution-2

Hi Sharepointers,

In one of my previous posts, I shared one of the two solutions for hiding checkbox in Upload.aspx page for a single document library.
This time I will share the second solution for the same issue.


Solution 2) Edit the Upload.aspx (application) page.

When u try to edit this page, it reflects on all the document libraries.
So we need to check to perform the action on a single library, and this check will be the List GUID from the URL.


<%

    if (Request.QueryString["List"] == "{651489A5-0C0D-4774-B6FF-B995BB06D82A}")

       {

                                OverwriteSingle.Visible=false;
                                UploadMultipleLink.Visible=false;
      }

%>


Substitute the above with your List ID.
For easily finding the GUID of a library u can refer my post here
That's it. Work is done :)

See u next time :)......

Monday, 30 December 2013

DataSheet View And People Picker issue

Hi All,
 Today i will share a weird issue i had gone through. Surely U will have to face it in some point of time.

Certainly everyone would have used Datasheet View. Have U ever checked People picker column in DataSheet View?

Bcoz when i tried, i got "Column Mismatch" error. When i tried the same entry in a Standard view, it got saved... So what is the prob and how to fix???

Finally, I fixed that after checking it for a Whole Day.
The Fix is ,
 U have to give atleast Read rights for the User to be entered in the People Picker.

People picker can validate user by name,email, id etc. in Standard View. But when you open it in Datasheet view then it does not validate the person by name,email. And instead of validating you will see small picker at person or group something like a choice column. You have to select from that.


Small one. But this will be certainly useful for you sometime..
Happy SharePointing...

Tuesday, 24 December 2013

Programmatically Hiding the "Overwrite existing files" checkbox for a single library

Hi All,
Last week i got a challenge for hiding the "Overwrite existing files" checkbox for a particular library.
First i thought it would be very easy , because my option was like adding a simple javascript to hide for that library.
I opened SharePoint Designer and tried adding javascript to that. It was not reflecting.. :(:(

On digging deep, I came to know that Upload.aspx is an application page and it is coming from the Layouts folder.
Ok finally i thought i found out the solution and tried changing the Upload.aspx page in the layouts folder. But the checkbox got hidden on all the pages..

Again i have to give a check on that so that it gets reflected for only one library. Finally i pointed out the List GUID in the URL of the page.

Then i tried and found 2 solutions for hiding checkbox for a single library.

Solution 1) Use this script in your master page

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready( function () {
        if (location.href.indexOf('0ABCE757-34A6-409D-A230-72382C924651') >= 0)
 // you must need to change GUID of your lib above
       {
            $("label:contains('Add as a new version to existing files')").prev('input').hide();
            $("label:contains('Add as a new version to existing files')").hide();
            $("a:contains('Upload Multiple Files...')").hide();
        }
    });
</script>

For easily finding the GUID of a library u can refer my post here


The second solution I will share on my next post. C ya!!!!


Thursday, 21 November 2013

Programmatically Export data from SharePoint List to Excel SpreadSheet

Hi All,
 Today I will give you the code for exporting data from SharePoint List to Excel.

Ok Without delay, directly going to the code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using System.IO;
using System.Web.UI;

namespace Excel
{
    class Program
    {
        private static DataTable dataTable;
        private static SPList list;

        static void Main(string[] args)
        {
            try
            {
                SPSite Osite = new SPSite("http://wf13staging.myhcl.com/sites/BPRCreatives/Creative%20CR%20Tracker");
                SPWeb oWeb = Osite.OpenWeb();
                string _siteUrl = oWeb.Url.ToString();
                if (!string.IsNullOrEmpty(_siteUrl))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(oWeb.Url))
                        {
                            if (site != null)
                            {
                                SPWeb web = site.OpenWeb();
                                if (web != null)
                                {
                                    #region Export List
                                    string _listName = "Excel List"; // Name of your List
                                    if (!string.IsNullOrEmpty(_listName))
                                    {
                                        list = web.Lists["Excel"];
                                        if (list != null)
                                        {
                                            dataTable = new DataTable();
                                            InitializeExcel(list, dataTable);
                                            string _schemaXML = list.DefaultView.ViewFields.SchemaXml;

                                            if (list.Items != null && list.ItemCount > 0)
                                            {
                                                foreach (SPListItem _item in list.Items)
                                                {
                                                    DataRow dr = dataTable.NewRow();
                                                    foreach (DataColumn _column in dataTable.Columns)
                                                    {
                                                        if (dataTable.Columns[_column.ColumnName] != null && _item[_column.ColumnName] != null)
                                                        {
                                                            dr[_column.ColumnName] = _item[_column.ColumnName].ToString();
                                                        }
                                                    }
                                                    dataTable.Rows.Add(dr);
                                                }
                                            }
                                        }
                                    }
                                    System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();
                                    grid.HeaderStyle.Font.Bold = true;
                                    grid.DataSource = dataTable;
                                    grid.DataBind();
          // U can modify the name of the excel file according to ur wish :)

                                    using (StreamWriter streamWriter = new StreamWriter(@"F:\Chandra\Excel" + list.Title + ".xls", false, Encoding.UTF8))
                                    {
                                        using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
                                        {
                                            grid.RenderControl(htmlTextWriter);
                                        }
                                    }

                                    #endregion
                                }
                            }
                        }
                    });
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public static void InitializeExcel(SPList list, DataTable _datatable)
        {
            if (list != null)
            {
                string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
                if (list.Items != null && list.ItemCount > 0)
                {
                    foreach (SPListItem _item in list.Items)
                    {
                        foreach (SPField _itemField in _item.Fields)
                        {
                            if (_schemaXML.Contains(_itemField.InternalName))
                            {
                                if (_item[_itemField.InternalName] != null)
                                {
                                    if (!_datatable.Columns.Contains(_itemField.InternalName))
                                    {
                                        _datatable.Columns.Add(new DataColumn(_itemField.StaticName, Type.GetType("System.String")));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}



That's it... Directly paste the code and Ur work is done.... ;)

Monday, 11 November 2013

SPListItem.Delete() Vs SPListItem.Recycle()

Hi All,
Good Day. Hope u all are doing good. Today we will see about 2 ways of deleting an item programmatically, which ofcourse are significant on their own ways.

While doing coding , 
SPListItem.Delete() is the most used method to delete an item. There is one more method - SPListItem.Recycle().

SPListItem.Recycle() :
When we use this method, the item gets moved to the Recycle Bin. So that , it can be restored if needed.

SPListItem.Delete() :
When we use this method, the item gets permanently deleted.

Under the hood :

Internally there isn’t much difference between the SPListItem.Delete and SPListItem.Recycle methods. Both call an internal SPListItem.Delete method with a different parameter which determines whether an item should be moved to the Recycle Bin or permanently deleted.


public override void Delete()   
{   
      if (this.HasExternalDataSource)   
      {   
            SPUtility.ValidateFormDigest();   
            string bdcid = (string) ((string) this.GetValue("BdcIdentity"));   
            this.ParentList.DataSource.DeleteItem(bdcid);   
      }   
      else   
      {   
            this.DeleteCore(DeleteOp.Delete);   
      }   
}   
public System.Guid Recycle()   
{   
      if (this.HasExternalDataSource)   
      {   
            SPExternalList.ThrowNotSupportedExceptionForMethod("Recycle", base.GetType());   
      }   
      return this.DeleteCore(DeleteOp.Recycle);   

}   

I hope you will be getting a good use of Recycle method in your forthcoming challenges.

Thanks for reading. 

Thursday, 17 October 2013

Users vs AllUsers vs SiteUsers in SharePoint

Hi All,

Today we will see about the Usercollections in SharePoint.Lets we see the Scenario

Scenario:

You want to get all the users who have access to the site. An SPWeb object exposes three different collections of users, as shown in this code below.

Code:

SPWeb web = SPContext.Current.Web;

SPUserCollection c1 = web.Users;

SPUserCollection c2 = web.AllUsers;

SPUserCollection c3 = web.SiteUsers;



Ok.... So what is the difference among these 3 collections.

The "Users" collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.


The "AllUsers" collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Sachin with the login of Company\Sachin that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Sandeep first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.

The "SiteUsers" collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

I hope now u will get an idea about these collections.

Obviously there, u will get this doubt.


-----What will the SharePoint UserInfoList contain - AllUsers or SiteUsers or Users?

And the answer is, Actually it contains cached information about the users logged in at least once to the site collection.

See u on my next post :) byeee....


Wednesday, 2 October 2013

DateTime Fields and CAML Queries , OffsetDays...

Hi Friends,
 Today I will share you some tips regarding CAML queries in SharePoint. Most of you would have played with CAML Queries.
OK , Now I will tel you how to deal with the Dates in CAML Query.

Scenario 1 : If you are checking for the items having "Due Date" field value as "Today", then this would be the query for u.

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today/>
</Value>
</Eq>
</Where>

Scenario 2 : what if u want something dynamic???
 EX: if u want to get the items, having "Due Date" 5 days before Today. For this you can't give a static day, because "Today" changes daily.
     Option is "OffsetDays".
The Query goes like this :

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today OffsetDays="-5" />  ----------> for future dates the value should be positive.
</Value>
</Eq>
</Where>

In many documents they have mentioned Offset instead of "OffsetDays"... better be careful while checking...

Have a good Day :)