OwnCloud Tweaks and tricks for C# Developers

By | October 27, 2020

What is owncloud

Visit https://owncloud.com/ for more details .

How to control OwnCloud management from C#

Download c# Client library for CSHARP

C# client library for ownCloud

Sample c# Codes Below to create User Account, Reset password, Share Folder, Create Public URL

public static void Main (string[] args)
            var c = new Client ("https://test.haneefputtur.com/owncloud10", "demo", "demo");
            /*Console.Write ("Testing DAV:List ... ");
			var list = c.List ("/");
			Console.WriteLine ("Received " + list.Count + " item(s)");
			foreach (var item in list) {
				if (!item.ContentType.Equals ("dav/directory")) {
					Console.Write ("Testing DAV:Downloading: " + item.Path + "/" + item.Name + " ... ");
					var data = c.Download (item.Path + "/" + item.Name);
					Console.WriteLine (data.Length + " Bytes received");
					BinaryReader reader = new BinaryReader (data);
					data.Position = 0;
					byte[] rdata = reader.ReadBytes ((int)data.Length);
					File.WriteAllBytes (item.Name, rdata);
					if (!c.Exists ("/demo"))
						c.CreateDirectory ("/demo");
					Console.Write ("Testing DAV:Uploading " + item.Name + " to /demo/ ... ");
					var status = c.Upload ("/demo/" + item.Name, new MemoryStream (rdata), item.ContentType);
					if (status)
						Console.WriteLine ("DONE");
						Console.WriteLine ("FAILED");
					Console.Write ("Testing DAV:Deleting " + item.Name + " from /demo/ ... ");
					status = c.Delete ("/demo/" + item.Name);
					if (status)
						Console.WriteLine ("DONE");
						Console.WriteLine ("FAILED");
				} else
            Console.Write ("Testing OCS:ListOpenRemoteShare ... ");
            if (!c.Exists("/demo"))

            var a = c.CreateUser("test", "D8!di7As1");
            a = c.CreateUser("demo001", "D8!di7As0");
            a = c.CreateGroup("Test123");
            a = c.AddUserToGroup("test", "Test123");

            var ps = c.ShareWithLink("/demo", Convert.ToInt32(OcsPermission.All), "demo", OcsBoolParam.True);
            var us = c.ShareWithUser("/demo", "test", Convert.ToInt32(OcsPermission.All), OcsBoolParam.False);
            var gs = c.ShareWithGroup("/demo", "Test123", Convert.ToInt32(OcsPermission.All));
            var result = c.IsShared("/demo") ? "is shared" : "is not shared";
			Console.WriteLine ("/demo " + result + ".");
            var r1 = c.SetUserAttribute("demo001", OCSUserAttributeKey.Password , "D8!di7Ab9"); // "D8!di7As1"

Caveats :

Even Though nuget packages are available to download I found that its giving exception when running each tasks. So better to download and create the DLL from source code.

Error in line 1 position 6. Expecting element ‘OCS’ from namespace ‘http://schemas.datacontract.org/2004/07/owncloudsharp.Types’.. Encountered ‘Element’  with name ‘ocs’, namespace ”.

Mounting Windows Share as Data Folder

Create a network share in windows and assign permission for a specific user to access this folder.

In My case : // , username : test , Password : haneef

Now open own cloud server through SSH and mount this share as drive

sudo apt-get install cifs-utils

sudo mkdir /media/ocshare

sudo nano /etc/fstab

// /media/ocshare cifs sec=ntlmv2,uid=www-data,username=test,password=test1234,iocharset=utf8,file_mode=0770,dir_mode=0770

sudo mount -a

Refer this documentation for Moving owncloud data folder : https://doc.owncloud.com/server/admin_manual/maintenance/manually-moving-data-folders.html#introduction

Manually Adding files to the User Folder

If you manually add files to owncloud folder run below command to sync owncloud database

sudo occ files:scan username

Adding File Scan to Cronjob

sudo crontab -u www-data -e

Select Nano as editor

Add Below lines

*/2 * * * * /var/www/owncloud/occ files:scan username

Deleting New User folder default files

When you create new user owncloud will add few default files. Use below commands to remove them

cd /var/www/owncloud/core/skeleton
sudo rm -r *

Setting zero quota for any owncloud user using c#

c.SetUserAttribute(“demo2”, OCSUserAttributeKey.Quota, “0”);