Sunday, January 20, 2013

Using FileStream for holding the share point document out side the Sharepoint

Recently I was doing one POC for bringing the share point library documents to external data base. All this is done by passing authentication details by network credentials object with in a web application.SQL server 2008 has this add on feature where we can hold the documents on file system and SQL server 2008 is providing the var binary max FILESTREAM data type for it. Before using this data type SQL server must need to configure to use this data type. I am giving some code snippets and the steps to bring share point library docs and put it in other SQL server 2008 db in filestream.



Introducing FILESTREAM

FILESTREAM is a SQL Server 2008 feature that lets you store unstructured BLOB data directly in the file system. FILESTREAM is not a data type; it is an attribute imposed on a varbinary column to indicate that the data is to be stored directly on the file system, thus maintaining transactional consistency.
A non-FILESTREAM storage format uses the buffer pool when the data pages are accessed. FILESTREAM uses the NT system cache for caching the file data. This approach helps reduce the effects that FILESTREAM data has on database engine performance. While the buffer pool is relieved of managing the varbinary(max) data pages, it is important to appreciate that the virtual address space (VAS) is still shared between FILESTREAM data and SQL Server data.
When using FILESTREAM, it is important to differentiate between traditional data (called row data) and FILESTREAM data.
 


1.Configuring the SQL server 2008 for using the FILESTREAM data type

To enable FILESTREAM storage at the instance level, Open the SQL Server 2008 Configuration Manager, Click on SQL Server Services on left side and then in the right side, right-click on the SQL Server instance you want to enable FILESTREAM storage on, choose Properties, then click on the FILESTREAM tab, and the following dialog box appears.


The next step is to open SQL Server Management Studio (SSMS) and run the following Transact-SQL code from a query window.

EXEC sp_configure filestream_access_level, 2
RECONFIGURE


FILESTREAM storage has now been enabled for the SQL Server instance.


The Transact-SQL code used to create a FILESTREAM-enabled database looks like this:
CREATE DATABASE MyFileStream_Database
ON
PRIMARY ( NAME = Info1,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\ MyFileStream_Database.mdf'),
FILEGROUP FileStreamGroup CONTAINS FILESTREAM( NAME = FILESTREAM_Data,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\ MyFileStream_Database)
LOG ON  ( NAME = Log1,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\ MyFileStream_Database.ldf')
GO




Create a new tables that include the VARBINARY(MAX) data type. The only difference between creating a standard VARBINARY(MAX) column in a table and a FILESTREAM-enabled VARBINARY(MAX) column is to add the keyword FILESTREAM after the VARBINARY(MAX). For example, to create a very simple table that can store FILESTREAM data, you can use code similar to this:
CREATE TABLE dbo.DocumentDetails

       Doc_ID UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE ,
       Name varchar(100),
       DocData VARBINARY(MAX) FILESTREAM
)


2 Bringing Share Point Data in Byte Array
below code snippet bring the file data in byte array when we pass the doc id and file url


public []byte GetStreamFromFile(string docid, , string fileurl, ClientContext clientContext)
        {
            byte[] bytesarr;
            try
            {
                List LibraryName=clientContext.Web.Lists.GetByTitle(lib);
                clientContext.Load(LibraryName);
                clientContext.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "" + fileurl + "
";
                Microsoft.SharePoint.Client.ListItemCollection collListItem = LibraryName.GetItems(camlQuery);
                clientContext.Load(collListItem, items => items.Include(item => item.Id, item => item["FileLeafRef"], item => item["LinkFilename"],
                                                 item => item["FileRef"], item => item["File_x0020_Size"], item => item["DocIcon"]));
                //clientContext.Load(collListItem);
                clientContext.ExecuteQuery();

                foreach (Microsoft.SharePoint.Client.ListItem oListItem in collListItem)
                {
                    string fileurl1 = (string)oListItem["FileRef"];
                    FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileurl1);
                                      
                    bytesarr = ReadFully(ffl.Stream);
                    obj.UpdateDocumentDetailForData(Convert.ToInt16(docid), bytesarr);
                
                }
            }
            catch (Exception ex)
            {
         //Catch exception here
            }
return bytesarr;

        }



3. Uploading file to filestream enabled table
 Thid code upload the file to related table. I have called the dml quereis method  directly here which calls the update store proc. Hope you can do that..  :)


//insert the file into database

public bool  UpdateDocumentDetailForData(int docid , byte[] data)

        {
           bool  val = false;
            try
            {
                //Add parameter of project id
                ArrayList lstParametName = new ArrayList();
                ArrayList lstParameterValue = new ArrayList();
                ArrayList lstParameterTypes = new ArrayList();
                lstParametName.Add("@ID");
                lstParameterValue.Add(docid.ToString());
                lstParameterTypes.Add(SqlDbType.VarChar);

                lstParametName.Add("@DocumentData");
                lstParameterValue.Add(data);
                lstParameterTypes.Add(SqlDbType.VarBinary);


                val = SqlHelper.executeProcIn("usp_UpdateDocumentDetailsByData", lstParametName, lstParameterTypes, lstParameterValue);

            }
            catch (Exception ex)
            {
            }
            return val;
 


4. Download file from filestream enabled table
Here  we are fetching the byte array from file stream enabled table and writing it to the local physical folder
  private void DownloadFileToLocal(DataTable dt)
        {
            try
            {
                Byte[] bytes = (Byte[])dt.Rows[0]["DocumentData"];
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                string ContentType = string.Empty;
                string filename = dt.Rows[0]["DocumentName"].ToString();
                string extension = dt.Rows[0]["DocumentCategory"].ToString(); ;
                if (extension == "txt")
                {

                    ContentType = "text/plain";

                }

                else if (extension == "gif")
                {

                    ContentType = "image/GIF";

                }

                else if (extension == "jpeg" || extension == "jpg")
                {

                    ContentType = "image/JPEG";

                }

                else if ((extension == "doc") || (extension == "docx"))
                {

                    ContentType = "application/x-msword";

                }

                else if ((extension == "xls") || (extension == "xlsx"))
                {

                    ContentType = "application/x-msexcel";

                }

                else if (extension == "pdf")
                {

                    ContentType = "application/pdf";

                }
                else if (extension == "vsd")
                {

                    ContentType = "application/vsd";

                }
                else
                {
                    ContentType = "application/" + extension;
                }

                Response.ContentType = ContentType;
                Response.AddHeader("content-disposition", "attachment;filename="
                + dt.Rows[0]["DocumentName"].ToString());
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
            catch (Exception ex)
            {
                LoggingHelper.getLogger().Error("Error occured in DownloadFileToLocal method of Transmittal template class of type " + ex);
            }
        }

Hope it will help you in this kind of scenario. Happy..coding..!















 


Tuesday, January 15, 2013

Top 10 Strategic Technology Trends For 2013

 Have look of top 10 technology that will rock in 2013. Article is taken from http://www.forbes.com/sites/ericsavitz/2012/10/23/gartner-top-10-strategic-technology-trends-for-2013/



Mobile device battles: Mobile experiences eclipse the desktop experience. Consumerization drives tablets into the enterprise. Cloud and mobile are mutually reinforcing trends. Bring your own device trend accelerates. In 2013, mobile devices will pass PCs to be most common Web access tools. By 2015, over 80% of handsets in mature markets will be smart phones. 20% of those will be Windows phones. By 2015, tablet shipments will be 50% of laptop shipments, with Windows 8 in third place behindApple and Android. Microsoft‘s share of overall client platform will fall to 60%, and could drop below 50%. In smartphones, Windows could pass RIM to be #3 player, and could be same size as Apple in units by 2015. Windows 8 will be “relatively niche,” with mostly appealing to enterprise buyers.
Mobile applications & HTML 5: Through 2014, JavaScript performance will push HTML5 and the browser as a mainstream application developer environment. There will be long shift to HTML5 from native apps as HTML5 becomes more capable. But native apps won’t disappear, and will always offer best experiences.
Personal Cloud: Cloud will be center of digital lives, for apps, content and preferences. Sync across devices. Services become more important; devices become less important.
Internet of Things: Internet of things is already here. Over 50% of Internet connections are things. In 2011,  over 15 billion things on the Web, with 50 billion+ intermittent connections. By 2020, over 30 billion connected things, with over 200 billion with intermittent connections. Key technologies here include embedded sensors, image recognition and NFC. By 2015, in more than 70% of enterprises, a single exec will oversee all Internet connected things. Becomes the Internet of Everything.
Hybrid IT and Cloud Computing: Changes role of IT.  IT departments must play more roles in coordinating IT related activities.
Strategic Big Data: Organizations need to focus on non-traditional data types and externa data sources. Hadoop and NoSQL gain momentum. Big data will meet social. Five richest big data sources on the Web include social graph, intent graph, consumption graph, interest graph and mobile graph. Concept of single corporate data warehouse is dead. Multiple systems need to be tied together.
Actionable Analytics: Cloud, packaged analytics and big data accelerates in 2013, 2014. Can now perform analytics and simulation on every action taken in business. Mobile devices will have access to the data, supporting business decision making.
Mainstream In-Memory Computing: Changes expectations, designs and architectures. Can boost performance and response times. Enables real-time self service business intelligence. SAP and others will accelerate delivery of applications in 2012/2013 to leverage in memory capability.
Integrated Ecosystems: More packaging of software and services to address infrastructure or application workload. There will be more shipment of “appliances,” with software delivered as hardware. New trend: virtual appliances, which Gartner sees gaining in popularity over the next five years.
 


http://www.forbes.com/sites/ericsavitz/2012/10/23/gartner-top-10-strategic-technology-trends-for-2013/

Monday, January 14, 2013

CSOM Usage Scinario

Usage scenarios for the CSOM

Following are examples of some kinds of apps that the CSOM supports. The CSOM can be used instead of the PSI for many scenarios:

    Develop apps that extend Project Server   The primary purpose of the CSOM is app development for Project Server 2013, where apps can be created for a wide variety of devices that include PCs, mobile devices, and tablets. Apps can be distributed within a private app catalog or in the public Office Store.
    Automate the creation or management of entities in Project Server   The CSOM can perform CRUD operations for entities such as projects, tasks, assignments, enterprise resources, custom fields, lookup tables, timesheets, event handlers, and workflow phases and stages. There are often cases where a custom app can save time with bulk or repetitive jobs.
    Get data in the published tables of the Project database   Because direct database access to the draft, published, and archive tables is not supported, you can use the CSOM to read data that is not available in the reporting tables or views. For example, get information about workflow stages, phases, and activities. To read data in the reporting tables, you can use OData queries.
    Validate statusing and timesheet data   Use the CSOM in local event handlers or remote event receivers for pre-events to validate assignment status or timesheet data that users enter, before the data is saved in Project Web App.


    Evaluate Project Server data in remote event receivers    A remote event receiver for a ProjectCreating pre-event can use Project Server data from the CSOM to help determine whether to cancel the event. For example, before creating a project, compare the project proposal with existing projects.
    Support declarative Project Server workflows   The CSOM enables Project Server workflows that are created in SharePoint Designer 2013. The CSOM supports workflow definitions that use Windows Workflow Foundation version 4 (WF4). (The PSI does not support WF4 workflows.)
    Create complex Project Server workflows   When you develop workflows with Visual Studio 2012, you can use the CSOM for complex actions within workflow stages or create custom workflow actions.

Request limits of the CSOM

The CSOM in Project Server 2013 is built on the CSOM implementation in SharePoint Server 2013 and inherits the limits for the maximum size of a request. SharePoint has a 2 MB limit for an operations request, and a 50 MB limit for the size of a submitted binary object. The request size is limited to protect the server from excessively long queues of operations and from processing delays for large binary objects.

For example, if you use the CSOM to create a project, and then edit the project to add 252 tasks with a minimum amount of information such as a short name, the task GUID, and a duration of 1d, the total amount of data in the DraftProject.Update request is less than 2 MB. But, if you try to add 253 such tasks to an empty project, the 2 MB limit is exceeded, and you get the following exception: Microsoft.SharePoint.Client.ServerException: The request uses too many resources

To capture the data in a CSOM request over HTTP or HTTPS, you can use a web debugging tool such as Fiddler (http://www.fiddler2.com). For a code example that implements a test for request size and includes a solution that breaks a large request into to smaller groups, see DraftProject.Update.