Pages

Saturday, November 7, 2015

Problem with using [Today] and [Me] in Calculated Columns

When you working with calculated columns you may have already figured out that you cannot use [Me] and [Today] functions in calculated column formulas and have seen “Calculated columns cannot contain volatile functions like Today and Me” error.

And you may have also seen a well-known tricky solution in some blogposts and sites like creating two columns named “Today” and “Me” to use in calculated column formulas. 

This may actually tricky but the worst case is that it has critical limitations and also it is not officially supported by Microsoft.

Regards to Christope...

Christope have already documented back this thing and have given two good solutions for this useless tricky thing with needful explanations.

Refer this links to see how he has achieved this posts’ title with overcome of limitations exist with [Today] and [Me] trick.




Sunday, October 11, 2015

Resetting a ID field of a list in SharePoint

When you are developing solutions you may have used to create plenty of items in the lists interact with you solution. But when you are trying to clear up and ready the contents of your solution for production environments you may have struggle with resetting Id field of list and libraries to restart numbering at 1 sometimes. In this scenario there’s no any list options or direct way to handle this case but there’s way.

This post going to describe the way which you can reset the Id field of a list. This can achieve by using the content database of the site which the list has created.

Follow this steps:

First you need to go the content database.(Go to Central Administration > Application Management. Under the Site Collections click View all site collections. Navigate to your site collection and get the Database Name)

Open the content database using MS SQL Server Management Studio.

Open New Query and run following query.

SELECT * FROM [Your_Content_Database_Name].[dbo].[AllListsAux] WHERE ListID='Your_List_GUID'

You can see a list called AllListsAux which is maintain ID details of all the lists of the site. You can see the NextAvailableId of your list there.

Now run the following query to update the NextAvailableId. This will reset the Id field of the list and will restart numbering at “1”.

UPDATE [Your_Content_Database_Name].[dbo].[AllListsAux] SET NextAvailableId=1 WHERE ListID='Your_List_GUID'

Check this Post see how to find the GUID of a list.

Note: You need to be careful when querying on and doing any modifications to content databases to avoid malfunctioning site contents. Also note that this not a recommended approach by Microsoft.

Saturday, October 10, 2015

How to Find the GUID of a SharePoint List

When you are working with SharePoint Lists, sometime you may need to get the ID (Guid) of a list.
For example, when setting the Task list to be used with SharePoint Designer Workflows.
Follow this steps:

  1. Go to the SharePoint List which you need to get ID.
  2. Select the Settings > List Settings menu option.
  3. Copy the entire URL from the browser address bar and paste into the notepad. It should looks like this:

*        Delete all before and including “List=”.
*        Replace “%7B” with “{”
*        Replace all “%2D” with “-“
*        Replace “%7D” with “}”

Now you have left with the GUID you need:
{EF97CD69-7D68-421F-A5CA-69768D83B328}

Sunday, October 4, 2015

Upload files to SharePoint Document Library and Sub Folders with Meta Data Programmatically

This post going to describe that how to create a Folder(Main Folder) within a Document Library and create a Sub Folder within Main Folder and upload files to Sub Folder.

private void UploadContractDocuments(string paraMainFolderName, string paraSubFolderName, 
            string paraContractNumber, DateTime paraStartDate, DateTime paraEndDate) 
        {
            try
            {
                string mainFolderName = paraMainFolderName;
                string subFolderName = paraSubFolderName;

                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    site.AllowUnsafeUpdates = false;

                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;

                        //Check whether file upload control contains a file
                        if (ContractFileUpload.HasFile)
                        {
                            SPDocumentLibrary documentLibrary = web.Lists["ContractDocuments"] as SPDocumentLibrary;

                            string FolderPath = documentLibrary.RootFolder.Url + "/" + mainFolderName;//Main folder path
                            string SubFolderPath = documentLibrary.RootFolder.Url + "/" + mainFolderName + "/" + subFolderName;//Sub folder path

                            //Check whether main folder already exist
                            if (!web.GetFolder(FolderPath).Exists)
                            {
                                //Create main folder if not exist
                                SPListItem newFolder = documentLibrary.Items.Add("", SPFileSystemObjectType.Folder, mainFolderName);
                                newFolder.Update();

                                //Create sub folder inside main folder
                                string subFolderUrl = web.Url + "/" + newFolder.Url;

                                SPListItem subFolder1 = documentLibrary.Items.Add(subFolderUrl, SPFileSystemObjectType.Folder, subFolderName.ToString());
                                subFolder1.Update();
                            }
                            else
                            {
                                //Check for subfolder if main folder is already exist
                                if (!web.GetFolder(SubFolderPath).Exists)
                                {
                                    //Create sub folder if not exist
                                    string subFolderUrl = FolderPath;

                                    SPListItem subFolder1 = documentLibrary.Items.Add(subFolderUrl, SPFileSystemObjectType.Folder, subFolderName.ToString());
                                    subFolder1.Update();
                                }
                            }

                            Stream fStream = RequestModuleFileUpload.PostedFile.InputStream;
                            byte[] _byteArray = new byte[fStream.Length];
                            fStream.Read(_byteArray, 0, (int)fStream.Length);
                            fStream.Close();

                            string fileName = RequestModuleFileUpload.PostedFile.FileName;

                            string _fileUrl = documentLibrary.RootFolder.Url + "/" + mainFolderName + "/" + subFolderName + "/" + Path.GetFileNameWithoutExtension(fileName) + Path.GetExtension(fileName);
                            bool IsOverwriteFile = true;

                            //Create meta data
                            var docMetadata = new Hashtable {
                                        { "ContractNumber", paraContractNumber },
                                        { "StartDate", paraStartDate},
                                        { "EndDate", paraEndDate }
                                        };

                            SPFile file = documentLibrary.RootFolder.Files.Add(_fileUrl, _byteArray, docMetadata, IsOverwriteFile);
                            SPListItem item = file.Item;

                            file.Update();
                            documentLibrary.Update();
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

Sunday, September 27, 2015

How to change site home page in SharePoint 2013

Method 1:-
  • Open site that you need to change the home page
  • Go to Site Actions and click Site Settings.
  • Under the Look and Feel section click Welcome Page
  • Click Browse to select the item from the site contents or you can type the URL of the content which you need to show as home page.
Method 2:-
  •  Go to the site page which you need to show as home page.
  •  Go the Page tab and click Make Homepage under the Page Actions sections. Site homepage will be changed to this page.
Method 3:-
  • Go to Site Pages in Site Contents.
  • Go to the Files tab and click New Document and select Wiki Page to create a new site page.
  • Under the page edit mode go to the Insert tab then click Web Part.
  • Now select the content under the App categories which you need to show in your homepage and click Save.
  • Go the Page tab and click Make Homepage under the Page Actions sections. Site homepage will be changed to this page.

Saturday, September 19, 2015

Backup and Restore a SharePoint Site Collection using PowerShell

In this article you are going to see that how to backup and restore SharePoint site. You need to use SharePoint management shell instead of regular Windows PowerShell.
You can use the Granular Backup option in SharePoint Central Administration to back up a site collection however you can only restore site collection using SharePoint Management Shell.

Simply follow this steps for:

Backup a Site Collection
  1. Open SharePoint 2010 Management Shell as Administrator (Run as administrator).
  2. Type the following command.
Backup-SPSite -Identity [SITECOLLECTIONURL] -Path [BACKUPFILEPATH] [-Force] [-NoSiteLock] [-UseSqlSnapshot] [-Verbose]

Replace [SITECOLLECTIONURL] with the URL of your site collection.
Replace [BACKUPFILEPATH] with the Path where your backup file should be saved.

Ex: Backup-SPSite -Identity http://servername/sites/mysite -Path D:\MySiteCollection.bak [-Force] [-NoSiteLock] [-UseSqlSnapshot] [-Verbose]

Your backup should be created on the selected path.

Restore a Site Collection
  1. Open SharePoint 2010 Management Shell as Administrator (Run as administrator).
  2. Type the following command.
Restore-SPSite -Identity [SITECOLLECTIONURL] -Path D:\MySiteCollectionBackup.bak –Force

Replace [SITECOLLECTIONURL] with the URL of your site collection which the backup should be restored.
Replace [BACKUPFILEPATH]  with the Path where your backup file is located.

Ex: Restore-SPSite -Identity http://servername/sites/newsite -Path D:\MySiteCollection.bak -Force

Your backup file should be restored on the provided URL.