URL to network file in SharePoint - c#

I have a column in a SharePoint list that I wish to make a link to a file on the network. The file location is generated in code so I need to write a CAML script to update the column.
Could someone please give me an example of what the value stored in the database would be? In this example the file location is \server\folder\file.txt. I would like the textual name to be the same if possible.

For your link you should use a column of the type "Hyperlink or Picture". This column type can handle a link and a description for it. To set these both values for a this field you would use the following code.
SPFieldUrlValue urlField = new SPFieldUrlValue();
urlField.Description = #"\\server\folder\file.txt";
urlField.Url = #"\\server\folder\file.txt";
yourListItem["yourLinkColumnName"] = urlField;
yourListItem.Update();
SharePoint will automatically convert the link from "\server\folder\file.txt" to "file://server/folder/file.txt". But be aware that SharePoint won't handle the permissions a user needs to access the file. It's just a link.

Related

Reading data from excel and send to SQL Server using Asp.Net MVC 5 C# with LINQ if its possible

Sorry with my English
I have an input file where the user will upload an Excel File, 1st I need to scan the extension of the file (.xlsx or .xls), after reading data from it and save the data in SQL Server.
Abour scan a extension I have this:
var ext = Path.GetExtension(file.FileName);
var allowedExtensions = new[] { ".xlsx", ".xls" };
if(allowedExtensions.Contains(ext)){ //codigo }
Now, my biggest question is to read the file data and send it to SQL Server.
The table has these columns:
ID
Registro
Nome
Ativo
I'm a newbie in the area, if that's simple, sorry :)
so you can read this thread, here is solidly explained how you can read data from excel : http://csharp.net-informations.com/excel/csharp-read-excel.htm
and about storing this data in DB, you can crate model class with the properties you already showed to us and then fill this properties with the data from excel and than insert them in db.
I recommend to use linktoexcel package method for that process. Its is easy and little code to get data from excel file. Please check this link and try it.
Get all the values from excel file by using linqtoexcel
Good luck
You can also use "Aspose.Cells" from nuget https://www.nuget.org/packages/Aspose.Cells/
to extract the cell information and fill your own objects with values if your intention is not to render the .xls/.xlsx but to extract the information then add to the DB.

Read/Write SharePoint User Field [Word 2010 VSTO]

I've been experimenting with reading SharePoint 2013 Site Column metadata from within a Word 2010 Application-level C# VSTO.
For testing I've set-up Site Columns for every type that SharePoint uses, then created a Document Content Type that ties to them all -- thus all these columns are embedded into the Word document (looks to be stored within customXml within the document file).
By reading from the _Document.ContentTypeProperties property within the VSTO's code, I can access most types, but I'm having difficulty accessing a 'Person or Group' Site Column's data -- I'm getting COM Exceptions attempting to read or write to an item's .Value property.
By looking at the XSD schema in customXml, I can see a single-value User column is made up of three values: DisplayName (type string), AccountType (type string) and AccountId (type UserId) -- however I don't see a way to read/write from/to this within the VSTO? Multi-value User columns appear to be completely different, and are made up of two string values: an ID (appears to be the SharePoint user's ID) and a string-based ID (or at least that's what I think the i:0#.w|domain\userid is, anyway).
Word itself can edit both single- and multi-valued User column data via the Document Panel, but only if Word is currently connected to SharePoint -- otherwise the functionality is disabled. I'd assume the same would be true for the VSTO, if I could access the values at all...
My two questions are:
Is there a way to read/write single- and multi-value User fields from within VSTO code (even if it's not via the _Document.ContentTypeProperties property)?
Is there a way to do Q1 when if not connected to SharePoint (if, say, the values are known to the code)?
(I've been somewhat overly verbose in case my workings so far are useful to someone else even if I get no answers; there doesn't seem to be a great amount of information about this anywhere)
With some provisos, I believe you can do read/update these fields using VSTO - although I haven't actually created a working example using VSTO, the same objects as I'd use in Word VBA are available - the code snippets below are VBA.
The person/group values that are displayed in the DIP are stored in a Custom XML Part, even when the SharePoint server is unavailable. So the problem is not modifying the values - it's a CRUD operation, in essence - but knowing what values you can use, particularly in the multi-valued case. If you know how to construct valid values (let's say you have an independent list of email addresses) then you can make the modifications locally. Personally, I don't know how I would construct a valid value for the multi-valued case so I'd basically have to contact the server.
So assuming you have the data you need to update locally...
When SharePoint serves a Word Document, it inserts/updates several Custom XML Parts. One contains a set of schemas (as you have discovered). Another contains the data. All you really need to do is access the correct Custom XML Part, find the XML Element corresponding to your SharePoint user/group column, then it's a CRUD operation on the subElements of that Element.
You can find the correct Custom XML Part using the appropriate namespace name, e.g.
Const metaPropDataUri as String = _
"http://schemas.microsoft.com/office/2006/metadata/properties"
Dim theDoc as Word.Document
Dim cxp as Office.CustomXMLPart
Dim cxps as Office.CustomXMLParts
Set theDoc = ActiveDocument
Set cxps = theDoc.CustomXMLParts.SelectByNamespace(metaPropDataUri)
If there is more than one part associate with that Namespace, I don't know for sure how to choose the correct one. AFAIK Word/Sharepoint only ever creates one, and experiments suggest that if there is another one, SharePoint works with the first one. So I use
Set cxp = cxps(1)
At this point you need to know the XML Element name of the person/group column. It may be the same as the external name (the one you can see in the SharePoint list), but if for example someone called the Sharepoint column "person group", the Element name will be "person_x0020_group". If the name isn't going to vary, you can get it from the schema XML as a one-off task. Or it may be easy to generate the correct element name from any given SharePoint name. Otherwise, you can get it dynamically from the Schema XML, which you can get (as a string) using
theDoc.ContentTypeProperties.SchemaXML
What you need to do then is find the element with attribute ma:displayName="the external name" and get the value of the name attribute. I would imagine that's quite straightforward using c#, a suitable XML object, and a bit of XPath, say
//xsd:element[#ma:displayName='person group'][1]/#name
which should return 'person_x0020_group'
You can then get the Element node for your data, e.g. something along the lines of
Dim cxn As Office.CustomXMLNode
Set cxn = cxp.SelectSingleNode("//*[name()='person_x0020_group'][1]")
Or you may find it's preferable to get the namespace Uri of the Elements in this Custom XML Part and use that to help you locate the correct node. The name is a long hex string assigned by SharePoint. You can get it from the Schema XML using, e.g.
//xsd:schema[1]/#targetNamespace
Once you have your node, you would use the known structures (i.e. the ones you have found in the Schemas) to get/modify/create child nodes as required.
of course you can. You should use the SharePoint Client-side Object model (CSOM) to manipulate SharePoint data from a location away from the server. The only thing you will need is the URL of your SharePoint site.
You can then connect through CSOM like this:
ClientContext context = new ClientContext("SITEURL");
Site site = context.Site;
Web web = context.Web;
context.Load(site);
context.Load(web);
context.ExecuteQuery();
See here an example to set a single user field:
First get the ID of the user through ensuring the username
u = context.Web.EnsureUser(UserOrGroupName);
context.Load(u);
context.ExecuteQuery();
To set the value, you can use this string format:
userid;#userloginname;#
To set the field use this:
item[myusercolumn] = "userid;#userloginname;#";
item.Update();
context.ExecuteQuery();
To set a multi user field, you can use the same code, just use ;# to concat the different usernames, such as:
item[myusercolumn] = "userid1;#userloginname1;#userid2;#userloginname2;#userid3;#userloginname3;#";
item.Update();
context.ExecuteQuery();
Hope this helps

Opening attachment stored in MS Access using C#

I have a column in my table stored in MS Access as a Attachment data type. It stores various files such as docx, pdf's etc.
I am trying to display a file from the table using a documentViewer (obtained from XtremeDocumentStudio .NET).
I also have a combo box on my form with a list of employee ID's. When a particular ID is selected from the combo box, I want the associated attachment with that employee to be displayed in the documentViewer.
I am using this query:
SELECT EmployeeAttachment FROM Employee WHERE EmployeeID = 2
I have been stuck on this problem for a while and am not sure on how to implement it. Any help or ideas on how I would do this would be greatly appreciated.
In order to get a faithful copy of the file from the Attachment field in the Access database you need to use the .SaveToFile method of an ACE DAO Field2 object. For details, see the related question:
Extracting files from an Attachment field in an Access database
Once you have extracted the file to disk (e.g., to System.IO.Path.GetTempPath) then you can tell the viewer control where to find it.

Sitecore WFFM check duplicate email

I am developing a form validation action for WFFM, which will not allow people to use same email for submitting multiple entries. So far, the only document I've got is the WFFM v2.3 Ref from Sitecore SDN, which only have few example of how to access submitted data form by form.
I don't know how to select data by using field value. So, my current solution is to retrieve all data from database and check all email fields; which doesn't seem right when putting in scale.
Do you have any code snippet that can help me add GridFilter like email="abc#def.com", if count > 0 definitely the email is duplicated?
Thank you.
Instead of finding the API supporting this very own demand, I found it easier to make a direct connection to the WFFM database and look up for what I want. Thank you for reading this.
The webforms database has 3 tables only. If for example, you want the list of email ids available in the 'Support Form' below:
Here's the query:
SELECT DISTINCT Value FROM [dbo].[Field]
WHERE FieldId = '5F5643B6-0535-49D8-B3C9-CF8E65A415C0'
Field Id corresponds to the field GUID of the form:
Ps. WebForms connections string should be available in App_Config\Include\forms.config.

How do I include Images in my Database in Visual Studio 2010

I'm fairly new to C# .Net. We're being taught it at University and are using Visual Studio to create windows forms. As a new part to the subject we're using databases, tables and datasets.
I opened a new Windows Form project and immediately added a new database to it. The table I want to create will have 2 columns - ImageID and the image itself. In what way do i add the image in to the box? I've tried full path, relative path and dragging the image in, but whatever I do i get the same error message....
Invalid Value
The changed value in this cell was not recognized as being valid.
.Net Framework Data Type: Byte[]
Error Message: You cannot use the result pane to set this Field data to
values other than NULL
Type a value appropriate for the data type or press ESC to cancel the
change
How can I have images in there? I just don't know how to use the image data type within the table. Any help is much appreciated.
A simpler approach is to store the image in the file system and only its path in the database. Basically you define a base folder:
string baseFolder = "c:\Program Files\MyApp\Images";
And use it to store relative paths in the database:
INSERT INTO ImagesTable (Name, Path)
Values ('German Shepherd', 'Dogs\german-shepherd.jpg')
Then, when you need to retrieve the image, you can do it like this:
string path = Path.Combine(baseFolder, 'Dogs\german-shepherd.jpg');
Image img = Image.FromFile(path);
In the following SO question you can find more information about the pros and cons of this approach:
Should I store my images in the database or folders?
you can store images in sql server 2008. Just create database table having column datatype "image".
now from .net code use the file upload control to select the image file and then convert the image parameter into byte[] before inserting image data into the database.

Categories