Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

How to change data of SPFile when connect to database of sharepoint 2013

$
0
0

With sharepoint 2013,
I try change content of a file in  Document Library by change content in database, by code:

// Run with user have permission log to DataBase of Sharepoint
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (var spSite = new SPSite(site.ID))
                    {
                        spConnection = new SqlConnection(spSite.ContentDatabase.DatabaseConnectionString);
                        spConnection.Open();

                        //string selectSql = string.Format("SELECT [Content] FROM dbo.DocStreams WHERE [DocId] = '{0}' and BSN = (Select Max(BSN) FROM dbo.DocStreams WHERE [DocId] = '{0}')", file.UniqueId);

                        //var selectCommand = new SqlCommand(selectSql, spConnection);
                        //oldContents = Encoding.UTF8.GetString((byte[])selectCommand.ExecuteScalar());

                        string updateCommandText = string.Format("UPDATE dbo.AllDocs SET Size = {0} WHERE Id = '{1}'", fileSize, file.UniqueId);
                        var updateCommand = new SqlCommand(updateCommandText, spConnection);
                        var contentDataAdapter = new SqlDataAdapter();
                        contentDataAdapter.UpdateCommand = updateCommand;
                        contentDataAdapter.UpdateCommand.ExecuteNonQuery();

                        updateCommandText = string.Format("UPDATE dbo.DocStreams SET [Content] = @BinaryData, Size = {1} WHERE [DocId] = '{0}' AND BSN = (Select Max(BSN) FROM dbo.DocStreams WHERE [DocId] = '{0}')", file.UniqueId, content.Length);
                        updateCommand = new SqlCommand(updateCommandText, spConnection);
                        updateCommand.Parameters.AddWithValue("@BinaryData", content);
                        updateCommand.ExecuteNonQuery();

                        spConnection.Close();
                    }
                });

then, i get error "Colbat exception" when read content of file.

Please help me



Viewing all articles
Browse latest Browse all 8089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>